home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / fgl105b.arc / USER1.TXT < prev    next >
Encoding:
Text File  |  1991-10-11  |  242.6 KB  |  5,572 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9. Fastgraph (tm)
  10.  
  11. User's Guide
  12.  
  13.  
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32.  
  33.  
  34.  
  35.  
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.                                                             Copyright (c) 1991
  49.                                                            All Rights Reserved
  50.  
  51.  
  52.  
  53.                                                            Ted Gruber Software
  54.                                                                   PO Box 13408
  55.                                                           Las Vegas, NV  89112
  56.  
  57.                                                                 (702) 735-1980
  58. Copyright (c) 1991 by Ted Gruber Software.
  59.  
  60. All rights reserved.  This publication and its associated software are sold
  61. without warranties, either expressed or implied, regarding their
  62. merchantability or fitness for any particular application or purpose.  The
  63. information in this publication is subject to change without notice and does
  64. not represent a commitment on the part of Ted Gruber Software.  In no event
  65. shall Ted Gruber Software be liable for any loss of profit or any other
  66. commercial damage, including but not limited to special, incidental,
  67. consequential, or other damages resulting from the use of or the inability to
  68. use this product, even if Ted Gruber Software has been notified of the
  69. possibility of such damages.
  70.  
  71.  
  72. First Printing, June 1991
  73.  
  74. Fastgraph version 2.00
  75. Fastgraph/Light version 1.00
  76.  
  77.  
  78.  
  79.  
  80. DFI is a registered trademark of Diamond Flower Electric Instrument Co., Inc.
  81.  
  82. Fastgraph and Fastgraph/Light are trademarks of Ted Gruber Software.
  83.  
  84. Hercules is a trademark of Hercules Computer Technology.
  85.  
  86. IBM, IBM PC, IBM PC/XT, IBM PC/AT, PS/2, PCjr, and PC-DOS are registered
  87. trademarks of International Business Machines, Inc.
  88.  
  89. Logitech is a trademark of LOGITECH, Inc.
  90.  
  91. Microsoft, Microsoft Mouse, and MS-DOS are registered trademarks of Microsoft
  92. Corporation.
  93.  
  94. QuickBASIC and QuickC are trademarks of Microsoft Corporation.
  95.  
  96. Tandy is a registered trademark of Tandy Corporation.
  97.  
  98. Turbo C is a registered trademark of Borland International, Inc.
  99.  
  100.  
  101. All other brand and product names mentioned in this publication are
  102. trademarks or registered trademarks of their respective holders.
  103.  
  104.                       T a b l e   o f   C o n t e n t s
  105.  
  106.  
  107. Chapter 1
  108.  
  109. Introduction  . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .    1
  110.      What is Fastgraph? . . . . . . . . . . . . . . . . . . . . . . . . .    2
  111.      Fastgraph/Light  . . . . . . . . . . . . . . . . . . . . . . . . . .    2
  112.      Prerequisite Knowledge . . . . . . . . . . . . . . . . . . . . . . .    2
  113.      Supported Compilers  . . . . . . . . . . . . . . . . . . . . . . . .    3
  114.      Installing Fastgraph . . . . . . . . . . . . . . . . . . . . . . . .    3
  115.      Fastgraph Naming Conventions . . . . . . . . . . . . . . . . . . . .    4
  116.      Compilation and Linking  . . . . . . . . . . . . . . . . . . . . . .    5
  117.           Microsoft C . . . . . . . . . . . . . . . . . . . . . . . . . .    5
  118.           Microsoft QuickC  . . . . . . . . . . . . . . . . . . . . . . .    6
  119.           Turbo C . . . . . . . . . . . . . . . . . . . . . . . . . . . .    8
  120.           Microsoft QuickBASIC  . . . . . . . . . . . . . . . . . . . . .    9
  121.           Microsoft FORTRAN . . . . . . . . . . . . . . . . . . . . . . .   11
  122.      Fastgraph/Light Video Driver . . . . . . . . . . . . . . . . . . . .   12
  123.  
  124. Chapter 2
  125.  
  126. PC and PS/2 Video Modes . . . . . . . . . . . . . . . . . . . . . . . . .   15
  127.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   16
  128.      Text Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   17
  129.      Graphics Modes . . . . . . . . . . . . . . . . . . . . . . . . . . .   19
  130.  
  131. Chapter 3
  132.  
  133. Initializing the Video Environment  . . . . . . . . . . . . . . . . . . .   27
  134.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   28
  135.      Establishing a Text Mode . . . . . . . . . . . . . . . . . . . . . .   28
  136.      Establishing a Graphics Mode . . . . . . . . . . . . . . . . . . . .   31
  137.      Summary of Video Initialization Routines . . . . . . . . . . . . . .   34
  138.  
  139. Chapter 4
  140.  
  141. Coordinate Systems  . . . . . . . . . . . . . . . . . . . . . . . . . . .   37
  142.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   38
  143.      Character Space  . . . . . . . . . . . . . . . . . . . . . . . . . .   38
  144.      Screen Space . . . . . . . . . . . . . . . . . . . . . . . . . . . .   39
  145.      World Space  . . . . . . . . . . . . . . . . . . . . . . . . . . . .   40
  146.      Conversion Routines  . . . . . . . . . . . . . . . . . . . . . . . .   41
  147.      Summary of Coordinate Routines . . . . . . . . . . . . . . . . . . .   42
  148.  
  149. Chapter 5
  150.  
  151. The Use of Color  . . . . . . . . . . . . . . . . . . . . . . . . . . . .   43
  152.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   44
  153.      Text Modes . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   44
  154.           Color Modes . . . . . . . . . . . . . . . . . . . . . . . . . .   44
  155.           Monochrome Mode . . . . . . . . . . . . . . . . . . . . . . . .   45
  156.      Graphics Modes . . . . . . . . . . . . . . . . . . . . . . . . . . .   46
  157.           CGA Color Modes . . . . . . . . . . . . . . . . . . . . . . . .   47
  158.           CGA Two-Color Mode  . . . . . . . . . . . . . . . . . . . . . .   48
  159.           Tandy and PCjr Modes  . . . . . . . . . . . . . . . . . . . . .   49
  160.  
  161.           Hercules Mode . . . . . . . . . . . . . . . . . . . . . . . . .   50
  162.           Hercules Low-Resolution Mode  . . . . . . . . . . . . . . . . .   51
  163.           EGA 200-Line Modes  . . . . . . . . . . . . . . . . . . . . . .   52
  164.           EGA Monochrome Mode . . . . . . . . . . . . . . . . . . . . . .   54
  165.           EGA Enhanced Mode . . . . . . . . . . . . . . . . . . . . . . .   55
  166.           VGA and MCGA Two-Color Mode . . . . . . . . . . . . . . . . . .   57
  167.           VGA 16-Color Mode . . . . . . . . . . . . . . . . . . . . . . .   59
  168.           VGA and MCGA 256-Color Modes  . . . . . . . . . . . . . . . . .   60
  169.      Defining All Palette Registers . . . . . . . . . . . . . . . . . . .   62
  170.      Virtual Colors . . . . . . . . . . . . . . . . . . . . . . . . . . .   63
  171.      A Multiple-Mode Example  . . . . . . . . . . . . . . . . . . . . . .   65
  172.      Summary of Color-Related Routines  . . . . . . . . . . . . . . . . .   67
  173.  
  174. Chapter 6
  175.  
  176. Graphics Fundamentals . . . . . . . . . . . . . . . . . . . . . . . . . .   69
  177.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   70
  178.      Clearing the Screen  . . . . . . . . . . . . . . . . . . . . . . . .   70
  179.      Clipping . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   70
  180.      Points . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   70
  181.      The Graphics Cursor  . . . . . . . . . . . . . . . . . . . . . . . .   73
  182.      Solid Lines  . . . . . . . . . . . . . . . . . . . . . . . . . . . .   74
  183.      Dashed Lines . . . . . . . . . . . . . . . . . . . . . . . . . . . .   76
  184.      Polygons, Circles, and Ellipses  . . . . . . . . . . . . . . . . . .   77
  185.      Solid Rectangles . . . . . . . . . . . . . . . . . . . . . . . . . .   80
  186.      Dithered Rectangles  . . . . . . . . . . . . . . . . . . . . . . . .   81
  187.      Region Fill  . . . . . . . . . . . . . . . . . . . . . . . . . . . .   88
  188.      Summary of Fundamental Graphics Routines . . . . . . . . . . . . . .   89
  189.  
  190. Chapter 7
  191.  
  192. Character Display Routines  . . . . . . . . . . . . . . . . . . . . . . .   93
  193.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .   94
  194.      Character Space  . . . . . . . . . . . . . . . . . . . . . . . . . .   94
  195.      Hardware Characters  . . . . . . . . . . . . . . . . . . . . . . . .   95
  196.      Conversion Routines  . . . . . . . . . . . . . . . . . . . . . . . .  100
  197.      Software Characters  . . . . . . . . . . . . . . . . . . . . . . . .  101
  198.      Bit-Mapped Characters  . . . . . . . . . . . . . . . . . . . . . . .  107
  199.      Summary of Character Display Routines  . . . . . . . . . . . . . . .  107
  200.  
  201. Chapter 8
  202.  
  203. Video Page Management . . . . . . . . . . . . . . . . . . . . . . . . . .  109
  204.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  110
  205.      Physical Pages and Virtual Pages . . . . . . . . . . . . . . . . . .  110
  206.      Pages With Special Meanings  . . . . . . . . . . . . . . . . . . . .  111
  207.      Some Simple Examples . . . . . . . . . . . . . . . . . . . . . . . .  111
  208.      Text Cursors . . . . . . . . . . . . . . . . . . . . . . . . . . . .  118
  209.      Obtaining Video Page Information . . . . . . . . . . . . . . . . . .  119
  210.      Considerations for Virtual Pages . . . . . . . . . . . . . . . . . .  120
  211.      Summary of Video Page Management Routines  . . . . . . . . . . . . .  120
  212.  
  213. Chapter 9
  214.  
  215. Images and Image Management . . . . . . . . . . . . . . . . . . . . . . .  121
  216.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  122
  217.      Mode-Independent Bit-Mapped Images . . . . . . . . . . . . . . . . .  122
  218.      Mode-Specific Bit-Mapped Images  . . . . . . . . . . . . . . . . . .  126
  219.  
  220.           Regular Images  . . . . . . . . . . . . . . . . . . . . . . . .  127
  221.           Clipped Images  . . . . . . . . . . . . . . . . . . . . . . . .  134
  222.           Reversed Images . . . . . . . . . . . . . . . . . . . . . . . .  135
  223.           Reversed Clipped Images . . . . . . . . . . . . . . . . . . . .  135
  224.           Some Examples . . . . . . . . . . . . . . . . . . . . . . . . .  135
  225.      Pixel Run Maps . . . . . . . . . . . . . . . . . . . . . . . . . . .  138
  226.      Display Patterns . . . . . . . . . . . . . . . . . . . . . . . . . .  144
  227.      Masking Maps . . . . . . . . . . . . . . . . . . . . . . . . . . . .  150
  228.      Retrieving Images  . . . . . . . . . . . . . . . . . . . . . . . . .  153
  229.      Byte Boundaries  . . . . . . . . . . . . . . . . . . . . . . . . . .  158
  230.      Image Transfer Routines  . . . . . . . . . . . . . . . . . . . . . .  159
  231.      Summary of Image Display Routines  . . . . . . . . . . . . . . . . .  167
  232.  
  233. Chapter 10
  234.  
  235. Animation Techniques  . . . . . . . . . . . . . . . . . . . . . . . . . .  169
  236.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  170
  237.      Simple Animation . . . . . . . . . . . . . . . . . . . . . . . . . .  170
  238.      XOR Animation  . . . . . . . . . . . . . . . . . . . . . . . . . . .  172
  239.      Static Frame Animation . . . . . . . . . . . . . . . . . . . . . . .  173
  240.      Dynamic Frame Animation  . . . . . . . . . . . . . . . . . . . . . .  176
  241.      Page Flipping  . . . . . . . . . . . . . . . . . . . . . . . . . . .  178
  242.      Summary of Animation Techniques  . . . . . . . . . . . . . . . . . .  179
  243.  
  244. Chapter 11
  245.  
  246. Special Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  181
  247.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  182
  248.      Screen Dissolving  . . . . . . . . . . . . . . . . . . . . . . . . .  182
  249.      Scrolling  . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  183
  250.      Changing the Screen Origin . . . . . . . . . . . . . . . . . . . . .  187
  251.      Summary of Special Effects Routines  . . . . . . . . . . . . . . . .  188
  252.  
  253. Chapter 12
  254.  
  255. Input Device Support  . . . . . . . . . . . . . . . . . . . . . . . . . .  191
  256.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  192
  257.      Keyboard Support . . . . . . . . . . . . . . . . . . . . . . . . . .  192
  258.           Reading Keystrokes  . . . . . . . . . . . . . . . . . . . . . .  194
  259.           Testing and Setting Key States  . . . . . . . . . . . . . . . .  196
  260.      Mouse Support  . . . . . . . . . . . . . . . . . . . . . . . . . . .  197
  261.           Initializing the Mouse  . . . . . . . . . . . . . . . . . . . .  197
  262.           Controlling the Mouse Cursor  . . . . . . . . . . . . . . . . .  198
  263.           Reporting the Mouse Status  . . . . . . . . . . . . . . . . . .  200
  264.           Defining the Mouse Cursor . . . . . . . . . . . . . . . . . . .  202
  265.      Joystick Support . . . . . . . . . . . . . . . . . . . . . . . . . .  210
  266.           Initializing Joysticks  . . . . . . . . . . . . . . . . . . . .  210
  267.           Reporting Joystick Status . . . . . . . . . . . . . . . . . . .  211
  268.           Keyboard Emulation  . . . . . . . . . . . . . . . . . . . . . .  213
  269.           Special Joystick Considerations . . . . . . . . . . . . . . . .  214
  270.      Summary of Input Routines  . . . . . . . . . . . . . . . . . . . . .  214
  271.  
  272. Chapter 13
  273.  
  274. Sound Effects . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  217
  275.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  218
  276.      Sound Sources  . . . . . . . . . . . . . . . . . . . . . . . . . . .  218
  277.      Synchronous Sound  . . . . . . . . . . . . . . . . . . . . . . . . .  218
  278.  
  279.      Asynchronous Sound . . . . . . . . . . . . . . . . . . . . . . . . .  223
  280.      Summary of Sound Routines  . . . . . . . . . . . . . . . . . . . . .  228
  281.  
  282. Chapter 14
  283.  
  284. Program Timing  . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  229
  285.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  230
  286.      Real-Time Routines . . . . . . . . . . . . . . . . . . . . . . . . .  230
  287.      Routines Dependent on the System Speed . . . . . . . . . . . . . . .  231
  288.      Summary of Timing Routines . . . . . . . . . . . . . . . . . . . . .  233
  289.  
  290. Chapter 15
  291.  
  292. Miscellaneous Routines  . . . . . . . . . . . . . . . . . . . . . . . . .  235
  293.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  236
  294.      Determining Available Memory . . . . . . . . . . . . . . . . . . . .  236
  295.      Choosing the Video Memory Update Function  . . . . . . . . . . . . .  237
  296.      Summary of Miscellaneous Routines  . . . . . . . . . . . . . . . . .  238
  297.  
  298. Appendix A
  299.  
  300. Fastgraph Utilities . . . . . . . . . . . . . . . . . . . . . . . . . . .  239
  301.      Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  240
  302.      SNAPSHOT Utility . . . . . . . . . . . . . . . . . . . . . . . . . .  240
  303.      CLIP Utility . . . . . . . . . . . . . . . . . . . . . . . . . . . .  241
  304.      CONVERT Utility  . . . . . . . . . . . . . . . . . . . . . . . . . .  243
  305.  
  306. Appendix B
  307.  
  308. Using Fastgraph from Assembly Language  . . . . . . . . . . . . . . . . .  245
  309.  
  310. Appendix C
  311.  
  312. Interrupts and Fastgraph  . . . . . . . . . . . . . . . . . . . . . . . .  249
  313.      Interrupts Used by Fastgraph . . . . . . . . . . . . . . . . . . . .  250
  314.      Extending the Time-of-Day Interrupt  . . . . . . . . . . . . . . . .  250
  315.  
  316. Appendix D
  317.  
  318. Contents of the Compiler-Specific Libraries . . . . . . . . . . . . . . .  253
  319.  
  320.  
  321.  
  322. Chapter 1
  323.  
  324. Introduction
  325. 2   Fastgraph User's Guide
  326.  
  327.  
  328. What is Fastgraph?
  329.  
  330.      Fastgraph is a library of more than 150 highly-optimized routines that
  331. are callable from high-level and assembly language programs running under the
  332. MS-DOS or PC-DOS operating systems.  This collection of routines provides a
  333. programmer with proven, powerful tools to take command of the PC and PS/2
  334. video environment.  In addition to its video support, Fastgraph also contains
  335. routines to perform keyboard, mouse, and joystick control, as well as music
  336. and sound capabilities.  Fastgraph is an ideal development tool for
  337. entertainment and educational software, presentation graphics products,
  338. scientific and engineering applications, CAD/CAM, animation, or just about
  339. any application that requires robust graphics.
  340.  
  341.      As its name implies, the most notable feature of Fastgraph is its speed.
  342. Virtually all of Fastgraph is written in 8086 assembly language, and each
  343. routine has been optimized by hand to provide maximum performance.
  344.  
  345.      Fastgraph supports all of the standard text and graphics video modes
  346. used by the IBM PC (PC, PC/XT, and PC/AT) and PS/2 families and compatible
  347. systems.  In addition, Fastgraph provides support for a commonly used video
  348. mode for the Tandy 1000 series computers and the PCjr.  Even though the
  349. graphics mode of the Hercules Graphics Card is not an IBM standard, its
  350. popularity has made it a de facto standard, and for this reason Fastgraph
  351. also supports it.  A complete discussion of all of Fastgraph's supported
  352. video modes appears in the next chapter.
  353.  
  354.  
  355. Fastgraph/Light
  356.  
  357.      Fastgraph/Light is a subset of Fastgraph.  It includes all of
  358. Fastgraph's features except the redefinable world space coordinate system and
  359. the routines pertaining to software characters.  Programs created using
  360. Fastgraph/Light are 100% source code compatible with Fastgraph.
  361.  
  362.      The most important difference between Fastgraph/Light and Fastgraph is
  363. the method of running a program created with the two products.  With
  364. Fastgraph, any of its routines used in your program are linked directly into
  365. the resulting .EXE file.  With Fastgraph/Light, however, this is not the
  366. case.  Instead, the Fastgraph/Light routines provide an interface to an
  367. external driver, called the Fastgraph/Light Video Driver, which must be
  368. loaded separately before running programs that call any Fastgraph/Light
  369. routines.  A further discussion of this driver begins on page 12.
  370.  
  371.      In this document, and in the accompanying Fastgraph Reference Manual,
  372. references to Fastgraph also apply to Fastgraph/Light unless stated
  373. otherwise.
  374.  
  375.  
  376. Prerequisite Knowledge
  377.  
  378.      Fastgraph is a programming tool, which means programmers are its
  379. intended audience.  For this reason, the Fastgraph User's Guide and the
  380. accompanying Fastgraph Reference Manual assume you have a knowledge of
  381. programming.  Virtually all of the examples in these two references are
  382. written in the C programming language, so a knowledge of C would be
  383. especially helpful.  However, the examples intentionally avoid using any of
  384.                                                   Chapter 1:  Introduction   3
  385.  
  386. C's features that might not be readily apparent to a programmer unfamiliar
  387. with C.  Additionally, a knowledge of converting numbers between binary,
  388. decimal, and hexadecimal is assumed.
  389.  
  390.  
  391. Supported Compilers
  392.  
  393.      You can use Fastgraph with any compilers or assemblers that use the same
  394. calling and naming conventions as the small, medium, or large memory models
  395. of the supported compilers.  Mixed language programming is allowed to the
  396. extent it is supported by the language translators and linker being used.
  397. The following compilers are supported at this time:
  398.  
  399.        Microsoft C (version 5.0 or later)
  400.        Microsoft QuickC (version 1.01 or later)
  401.        Borland's Turbo C (version 2.0 or later)
  402.        Microsoft QuickBASIC (version 4.0 or later)
  403.        Microsoft FORTRAN (version 4.0 or later)
  404.  
  405. The listed version numbers are the compiler versions under which Fastgraph
  406. was developed and tested.  Fastgraph may or may not work with earlier
  407. versions of these compilers.  The use of Fastgraph from assembly language
  408. programs is addressed in Appendix B.
  409.  
  410.  
  411. Installing Fastgraph
  412.  
  413.      This section explains how to use the INSTALL program to load Fastgraph
  414. (or Fastgraph/Light) and its related files on a hard disk.  The installation
  415. program lets you select the compilers and memory models you wish to use with
  416. Fastgraph.  It also gives you the opportunity to load many example Fastgraph
  417. programs specific to the compilers you choose.
  418.  
  419.      Before you start the installation, you should use the DOS commands COPY
  420. or DISKCOPY to make working copies of the distribution disks (refer to your
  421. DOS reference manual if you are not familiar with these commands).  Once you
  422. have created the working copies, store the original disks in a safe place. 
  423. Install Fastgraph from the working copies you just created.
  424.  
  425.      For simplicity, we'll assume you are installing Fastgraph from the
  426. diskette drive A: to the hard drive C:, but you can of course use any
  427. available drives.  The Fastgraph distribution disk labeled "Installation and
  428. Utilities" contains Fastgraph's INSTALL program.  Place this disk in the A:
  429. drive, make A: your current drive, and enter the command INSTALL, as shown
  430. below.
  431.  
  432.  
  433.                                   C> A:
  434.                                   A> INSTALL
  435.  
  436.  
  437. From this point, just follow the directions on each screen.  At any time, you
  438. can press the Esc key to abort the installation.
  439. 4   Fastgraph User's Guide
  440.  
  441.      You can install support for additional compilers or memory models at any
  442. time.  If you choose to do this, you should use the command INSTALL /L to
  443. avoid copying the files common to all compilers and memory models.
  444.  
  445.  
  446. Fastgraph Naming Conventions
  447.  
  448.      The names of all Fastgraph routines begin with the three characters
  449. "fg_".  This prefix helps identify Fastgraph routines within a program, and
  450. it also reduces the possibility of name conflicts that might otherwise occur
  451. between Fastgraph and other third party libraries.
  452.  
  453.      Because QuickBASIC does not permit underscores in identifiers, the
  454. QuickBASIC versions of Fastgraph routines begin with the two characters "FG".
  455. For example, the fg_version routine is named FGversion in the QuickBASIC
  456. libraries.  All subsequent references to Fastgraph routines in this manual
  457. and the accompanying Fastgraph Reference Manual will use the fg_ naming
  458. convention instead of the QuickBASIC names.
  459.                                                   Chapter 1:  Introduction   5
  460.  
  461.  
  462. Compilation and Linking
  463.  
  464.      To build an executable (.EXE) file for a program that uses Fastgraph
  465. routines, first compile or assemble the program using the small, medium, or
  466. large memory model.  This step produces an object (.OBJ) file, which is then
  467. linked with Fastgraph and any other object libraries to produce an executable
  468. file.  The following sections show the simplest compilation and linking
  469. procedures for each of the supported compilers.
  470.  
  471.      Example 1-1 is a C program that uses the Fastgraph routine fg_version to
  472. display the version number for your copy of Fastgraph (QuickBASIC and FORTRAN
  473. versions of this program are presented in the sections that discuss those
  474. compilers).  If you loaded the example programs when you installed Fastgraph,
  475. the file \FG\EXAMPLES\01-01.C contains the source code for this program.  You
  476. can use example 1-1 to test the compilation and linking process for the
  477. memory models and compilers you'll be using with Fastgraph.
  478.  
  479.                                  Example 1-1.
  480.  
  481.       #include <stdio.h>
  482.       #include <fastgraf.h>
  483.  
  484.       main()
  485.       {
  486.          int major;
  487.          int minor;
  488.  
  489.          fg_version(&major,&minor);
  490.          printf("This is version %d.%2.2d of Fastgraph.\n",major,minor);
  491.       }
  492.  
  493.  
  494. The file FASTGRAF.H contains the C function prototypes for each Fastgraph
  495. routine.  It should reside in the directory where the compiler normally
  496. searches for other .H files.  For the Microsoft C or QuickC compilers,
  497. FASTGRAF.H can instead reside in any of the directories specified by the
  498. INCLUDE environment variable.
  499.  
  500.  
  501. Microsoft C
  502.  
  503.      Microsoft C programs are compiled and linked by entering a CL command at
  504. the DOS command prompt.  The format of the CL command for compiling a program
  505. and linking it with Fastgraph is
  506.  
  507.  
  508.         CL <model> <filename> /link <fg_library> [<fg_extended>] [/E]
  509.  
  510.  
  511. where:
  512.  
  513.      <model>        specifies the compiler memory model you'll be using.
  514.                     It must be either /AS (for the small model), /AM
  515.                     (for the medium model), or /AL (for the large
  516.                     model).
  517. 6   Fastgraph User's Guide
  518.  
  519.      <filename>     is the name of the file containing your program.  It
  520.                     may include a path specification.
  521.  
  522.      <fg_library>   is the name of a standard Fastgraph/Light or
  523.                     Fastgraph library.  For Fastgraph/Light, the library
  524.                     name is FGLS (for the small model), FGLM (for the
  525.                     medium model), or FGLL (for the large model).  For
  526.                     Fastgraph, the library name is FGS (for the small
  527.                     model), FGM (for the medium model), or FGL (for the
  528.                     large model).
  529.  
  530.      <fg_extended>  is the name of an optional compiler-specific
  531.                     extended Fastgraph library.  You need to specify an
  532.                     extended library name only if your program calls any
  533.                     of the Fastgraph routines listed in Appendix D.  The
  534.                     extended library name is FGMSCS (for the small
  535.                     model), FGMSCM (for the medium model), or FGMSCL
  536.                     (for the large model).  Fastgraph/Light does not use
  537.                     extended libraries.
  538.  
  539. The /E linker option is not required but will produce a smaller .EXE file if
  540. specified.
  541.  
  542.      For example, to compile the program 01-01.C under the medium memory
  543. model and then link it with Fastgraph, you could use the following CL
  544. command:
  545.  
  546.  
  547.                       CL /AM 01-01.C /link FGM FGMSCM /E
  548.  
  549.  
  550. Although we specified the extended library name FGMSCM on the command line,
  551. we didn't need to in this example because the program doesn't call any of the
  552. compiler-specific Fastgraph routines listed in Appendix D.  If you were using
  553. Fastgraph/Light instead of Fastgraph, the CL command would be:
  554.  
  555.  
  556.                          CL /AM 01-01.C /link FGLM /E
  557.  
  558.  
  559. For more information about memory models or other compilation and linking
  560. options, please refer to the Microsoft C Optimizing Compiler User's Guide,
  561. published by Microsoft Corporation.
  562.  
  563.  
  564. Microsoft QuickC
  565.  
  566.      Microsoft QuickC allows you to compile and link a program directly from
  567. the DOS command line, or from within its programming environment.  To use
  568. Fastgraph from QuickC's programming environment, just specify the quick
  569. library name FGQC when starting QuickC, as shown below.
  570.  
  571.  
  572.                                   QC /lFGQC
  573.  
  574.  
  575. If you are using Fastgraph/Light, use the library name FGLQC instead of FGQC.
  576.                                                   Chapter 1:  Introduction   7
  577.  
  578.  
  579.      You can also compile and link a QuickC program from the DOS command line
  580. using the QCL command.  The format of the QCL command for compiling a program
  581. and linking it with Fastgraph is
  582.  
  583.  
  584.         QCL <model> <filename> /link <fg_library> [<fg_extended>] [/E]
  585.  
  586.  
  587. where:
  588.  
  589.      <model>        specifies the compiler memory model you'll be using.
  590.                     It must be either /AS (for the small model), /AM
  591.                     (for the medium model), or /AL (for the large
  592.                     model).
  593.  
  594.      <filename>     is the name of the file containing your program.  It
  595.                     may include a path specification.
  596.  
  597.      <fg_library>   is the name of a standard Fastgraph/Light or
  598.                     Fastgraph library.  For Fastgraph/Light, the library
  599.                     name is FGLS (for the small model), FGLM (for the
  600.                     medium model), or FGLL (for the large model).  For
  601.                     Fastgraph, the library name is FGS (for the small
  602.                     model), FGM (for the medium model), or FGL (for the
  603.                     large model).
  604.  
  605.      <fg_extended>  is the name of an optional compiler-specific
  606.                     extended Fastgraph library.  You need to specify an
  607.                     extended library name only if your program calls any
  608.                     of the Fastgraph routines listed in Appendix D.  The
  609.                     extended library name is FGMSCS (for the small
  610.                     model), FGMSCM (for the medium model), or FGMSCL
  611.                     (for the large model).  Fastgraph/Light does not use
  612.                     extended libraries.
  613.  
  614. The /E linker option is not required but will produce a smaller .EXE file if
  615. specified.
  616.  
  617.      For example, to compile the program 01-01.C under the medium memory
  618. model and then link it with Fastgraph, you could use the following QCL
  619. command:
  620.  
  621.  
  622.                      QCL /AM 01-01.C /link FGM FGMSCM /E
  623.  
  624.  
  625. Although we specified the extended library name FGMSCM on the command line,
  626. we didn't need to in this example because the program doesn't call any of the
  627. compiler-specific Fastgraph routines listed in Appendix D.  If you were using
  628. Fastgraph/Light instead of Fastgraph, the QCL command would be:
  629.  
  630.  
  631.                         QCL /AM 01-01.C /link FGLM /E
  632.  
  633. 8   Fastgraph User's Guide
  634.  
  635. For more information about memory models or other compilation and linking
  636. options, please refer to the Microsoft QuickC Compiler Programmer's Guide,
  637. published by Microsoft Corporation.
  638.  
  639.  
  640. Turbo C
  641.  
  642.      Turbo C allows you to compile and link a program directly from the DOS
  643. command line, or from within its integrated development environment.  To use
  644. Fastgraph from Turbo C's integrated development environment, you must make
  645. sure the compiler options match one of Fastgraph's available memory models
  646. (small, medium, or large), and then create a project file that links with the
  647. corresponding Fastgraph libraries (as listed below).
  648.  
  649.      You can also compile and link a Turbo C program from the DOS command
  650. line using the TCC command.  The format of the TCC command for compiling a
  651. program and linking it with Fastgraph is
  652.  
  653.  
  654.              TCC <model> <filename> <fg_library> [<fg_extended>]
  655.  
  656.  
  657. where:
  658.  
  659.      <model>        specifies the compiler memory model you'll be using.
  660.                     It must be either -ms (for the small model), -mm
  661.                     (for the medium model), or -ml (for the large
  662.                     model).
  663.  
  664.      <filename>     is the name of the file containing your program.  It
  665.                     may include a path specification.
  666.  
  667.      <fg_library>   is the name of a standard Fastgraph/Light or
  668.                     Fastgraph library.  For Fastgraph/Light, the library
  669.                     name is FGLS.LIB (for the small model), FGLM.LIB
  670.                     (for the medium model), or FGLL.LIB (for the large
  671.                     model).  For Fastgraph, the library name is FGS.LIB
  672.                     (for the small model), FGM.LIB (for the medium
  673.                     model), or FGL.LIB (for the large model).
  674.  
  675.      <fg_extended>  is the name of an optional compiler-specific
  676.                     extended Fastgraph library.  You need to specify an
  677.                     extended library name only if your program calls any
  678.                     of the Fastgraph routines listed in Appendix D.  The
  679.                     extended library name is FGTCS.LIB (for the small
  680.                     model), FGTCM.LIB (for the medium model), or
  681.                     FGTCL.LIB (for the large model).  Fastgraph/Light
  682.                     does not use extended libraries.
  683.  
  684.      For example, to compile the program 01-01.C under the medium memory
  685. model and then link it with Fastgraph, you could use the following TCC
  686. command:
  687.  
  688.  
  689.                       TCC -mm 01-01.C FGM.LIB FGTCM.LIB
  690.  
  691.                                                   Chapter 1:  Introduction   9
  692.  
  693. Although we specified the extended library name FGTCM.LIB on the command
  694. line, we didn't need to in this example because the program doesn't call any
  695. of the compiler-specific Fastgraph routines listed in Appendix D.  If you
  696. were using Fastgraph/Light instead of Fastgraph, the TCC command would be:
  697.  
  698.  
  699.                            TCC -mm 01-01.C FGLM.LIB
  700.  
  701.  
  702. For more information about memory models or other compilation and linking
  703. options, please refer to the Turbo C User's Guide and the Turbo C Reference
  704. Guide, both published by Borland International.
  705.  
  706.  
  707. Microsoft QuickBASIC
  708.  
  709.      Example 1-2 is a QuickBASIC program that uses the Fastgraph routine
  710. FGversion to display the version number for your copy of Fastgraph.  If you
  711. loaded the example programs when you installed Fastgraph, the file
  712. \FG\EXAMPLES\01-02.BAS contains the source code for this program.  You can
  713. use example 1-2 to test the QuickBASIC compilation and linking process for
  714. Fastgraph.
  715.  
  716.                                  Example 1-2.
  717.  
  718.           REM $INCLUDE: 'fastgraf.bi'
  719.  
  720.           DEFINT A-Z
  721.  
  722.           FGversion Major, Minor
  723.           Version! = Major + Minor*0.01
  724.  
  725.           PRINT USING "This is version #.## of Fastgraph."; Version!
  726.  
  727.           END
  728.  
  729.  
  730. You must include the DECLARE commands in the file FASTGRAF.BI at the
  731. beginning of each QuickBASIC module.  This file should reside in the
  732. directory where the compiler normally searches for other .BI files, or in any
  733. of the directories specified by the INCLUDE environment variable.  The
  734. DECLARE commands in this file automatically provide the calling convention
  735. and naming convention for each Fastgraph routine.  In addition, they relieve
  736. the QuickBASIC programmer of distinguishing arguments passed by value from
  737. those passed by reference.
  738.  
  739.      Microsoft QuickBASIC allows you to compile and link a program directly
  740. from the DOS command line, or from within its programming environment.  To
  741. use Fastgraph from QuickBASIC's programming environment, just specify the
  742. quick library name FGQB when starting QuickBASIC, as shown below.
  743.  
  744.  
  745.                                   QB /lFGQB
  746.  
  747.  
  748. If you are using Fastgraph/Light, use the library name FGLQB instead of FGQB.
  749. 10   Fastgraph User's Guide
  750.  
  751.      You can also compile and link a QuickBASIC program from the DOS command
  752. line using the BC and LINK commands.  The format of these commands for
  753. compiling a program and linking it with Fastgraph is
  754.  
  755.  
  756.                   BC <filename>;
  757.                   LINK [/E] <object_file>,,NUL,<fg_library>
  758.  
  759.  
  760. where:
  761.  
  762.      <filename>     is the name of the file containing your program.  It
  763.                     may include a path specification.
  764.  
  765.      <object_file>  is the name of the object file produced by the BC
  766.                     command.  By default, this will be the same as
  767.                     <filename>, but with an extension of .OBJ rather
  768.                     than .BAS.
  769.  
  770.      <fg_library>   is the name of the standard Fastgraph/Light or
  771.                     Fastgraph library.  For Fastgraph/Light, the library
  772.                     name is FGLQB.  For Fastgraph, the library name is
  773.                     FGQB.  QuickBASIC does not use extended Fastgraph
  774.                     libraries.
  775.  
  776. The /E linker option is not required but will produce a smaller .EXE file if
  777. specified.
  778.  
  779.      For example, to compile the program 01-02.BAS and then link it with
  780. Fastgraph, you could use the following commands:
  781.  
  782.  
  783.                          BC 01-02.BAS;
  784.                          LINK /E 01-02.OBJ,,NUL,FGQB
  785.  
  786.  
  787. If you were using Fastgraph/Light instead of Fastgraph, the commands would
  788. be:
  789.  
  790.  
  791.                          BC 01-02.BAS;
  792.                          LINK /E 01-02.OBJ,,NUL,FGLQB
  793.  
  794.  
  795. For more information about other compilation and linking options, please
  796. refer to the Microsoft QuickBASIC: Programming in BASIC manual, published by
  797. Microsoft Corporation.
  798.  
  799.      All of the remaining example programs in the Fastgraph User's Guide are
  800. written in the C programming language.  However, when you install Fastgraph
  801. for Microsoft QuickBASIC, the installation procedure copies QuickBASIC
  802. versions of the example programs to the \FG\EXAMPLES directory.
  803.                                                  Chapter 1:  Introduction   11
  804.  
  805.  
  806. Microsoft FORTRAN
  807.  
  808.      Example 1-3 is a FORTRAN program that uses the Fastgraph routine
  809. fg_version to display the version number for your copy of Fastgraph.  If you
  810. loaded the example programs when you installed Fastgraph, the file
  811. \FG\EXAMPLES\01-03.FOR contains the source code for this program.  You can
  812. use example 1-3 to test the FORTRAN compilation and linking process for the
  813. memory models you'll be using with Fastgraph.
  814.  
  815.                                  Example 1-3.
  816.  
  817.      $INCLUDE: '\FG\INTRFACE.FOR'
  818.  
  819.            PROGRAM MAIN
  820.  
  821.            INTEGER*2 MAJOR
  822.            INTEGER*2 MINOR
  823.  
  824.            CALL FG_VERSION (MAJOR, MINOR)
  825.  
  826.            WRITE (6,10) MAJOR, MINOR
  827.      10    FORMAT (' This is version ', I1, '.', I2.2, ' of Fastgraph.')
  828.  
  829.            STOP ' '
  830.            END
  831.  
  832.  
  833. You must include the INTERFACE statements in the file INTRFACE.FOR at the
  834. beginning of your program (this file should reside in the \FG directory).
  835. The INTERFACE statements in this file automatically provide the calling
  836. convention and naming convention for each Fastgraph routine.  In addition,
  837. they relieve the FORTRAN programmer of distinguishing arguments passed by
  838. value from those passed by reference.
  839.  
  840.      Microsoft FORTRAN programs are compiled and linked by entering an FL
  841. command at the DOS command prompt.  The format of the FL command for
  842. compiling a program and linking it with Fastgraph is
  843.  
  844.  
  845.  
  846.  FL /FPi /4I2 /4Nt <model> <filename> /link <fg_library> [<fg_extended>] [/E]
  847.  
  848.  
  849.  
  850. where:
  851.  
  852.      <model>        specifies the compiler memory model you'll be using.
  853.                     It must be either /AM (for the medium model) or /AL
  854.                     (for the large model).  Microsoft FORTRAN does not
  855.                     support the small model.
  856.  
  857.      <filename>     is the name of the file containing your program.  It
  858.                     may include a path specification.
  859. 12   Fastgraph User's Guide
  860.  
  861.      <fg_library>   is the name of a standard Fastgraph/Light or
  862.                     Fastgraph library.  For Fastgraph/Light, the library
  863.                     name is FGLM (for the medium model) or FGLL (for the
  864.                     large model).  For Fastgraph, the library name is
  865.                     FGM (for the medium model) or FGL (for the large
  866.                     model).
  867.  
  868.      <fg_extended>  is the name of an optional compiler-specific
  869.                     extended Fastgraph library.  You need to specify an
  870.                     extended library name only if your program calls any
  871.                     of the Fastgraph routines listed in Appendix D.  The
  872.                     extended library name is FGMSFM (for the medium
  873.                     model) or FGMSFL (for the large model).
  874.                     Fastgraph/Light does not use extended libraries.
  875.  
  876. The /E linker option is not required but will produce a smaller .EXE file if
  877. specified.
  878.  
  879.      For example, to compile the program 01-03.FOR under the medium memory
  880. model and then link it with Fastgraph, you could use the following FL
  881. command:
  882.  
  883.  
  884.              FL /FPi /4I2 /4Nt /AM 01-03.FOR /link FGM FGMSFM /E
  885.  
  886.  
  887. Although we specified the extended library name FGMSFM on the command line,
  888. we didn't need to in this example because the program doesn't call any of the
  889. compiler-specific Fastgraph routines listed in Appendix D.  If you were using
  890. Fastgraph/Light instead of Fastgraph, the FL command would be:
  891.  
  892.  
  893.                 FL /FPi /4I2 /4Nt /AM 01-03.FOR /link FGLM /E
  894.  
  895.  
  896. For more information about memory models or other compilation and linking
  897. options, please refer to the Microsoft FORTRAN Optimizing Compiler User's
  898. Guide, published by Microsoft Corporation.
  899.  
  900.      All of the remaining example programs in the Fastgraph User's Guide are
  901. written in the C programming language.  However, when you install Fastgraph
  902. for the Microsoft FORTRAN compiler, the installation procedure copies FORTRAN
  903. versions of the example programs to the \FG\EXAMPLES directory.
  904.  
  905.  
  906. Fastgraph/Light Video Driver
  907.  
  908.      As mentioned earlier, running any program created with Fastgraph/Light
  909. requires an external program called the Fastgraph/Light Video Driver.  The
  910. video driver is a terminate and stay resident program (TSR) that provides an
  911. interface between your program and Fastgraph.  Once loaded, the video driver
  912. uses about 60,000 bytes of conventional memory.
  913.  
  914.      To load the video driver, enter the command FGDRIVER at the DOS command
  915. prompt (assuming FGDRIVER.EXE is in the current directory, or the \FG
  916. directory is in your DOS path specification).  The driver will display a
  917. message indicating whether or not it was loaded successfully.  After you load
  918.                                                  Chapter 1:  Introduction   13
  919.  
  920. the driver, just run a program created with Fastgraph/Light as would run any
  921. other program.  If you try running a program that uses Fastgraph/Light
  922. without first loading the video driver, the message "Fastgraph/Light video
  923. driver not installed" will appear.
  924.  
  925.      You don't need to load the driver before running each program, just once
  926. per system boot (in fact, the driver will display an "already loaded" message
  927. if you try to load it more than once).  If you want to unload the video
  928. driver, just enter FGDRIVER /U at the DOS command prompt.  The unload
  929. operation will work completely only if the video driver was the last TSR
  930. loaded.  If it wasn't the last TSR, the driver will still unload, but the
  931. memory it uses will not be released back to DOS.
  932. 14   Fastgraph User's Guide
  933.  
  934.  
  935. Chapter 2
  936.  
  937. PC and PS/2 Video Modes
  938. 16   Fastgraph User's Guide
  939.  
  940.  
  941. Overview
  942.  
  943.      In the PC and PS/2 worlds, the method by which information appears on
  944. the computer's display screen is determined by the video mode currently in
  945. effect.  The video modes have different resolutions, different character or
  946. pixel attributes, different video memory structures, and other inherent
  947. hardware differences.  However, you do not need an in-depth knowledge of
  948. these video internals, because Fastgraph handles the necessary details.
  949.  
  950.      The PC and PS/2 video modes may be separated into two major classes:
  951. text modes and graphics modes.  In text modes, the display screen is divided
  952. into character cells.  There are 25 rows and either 40 or 80 columns of
  953. cells, and in each cell we can store any of the 256 characters in the IBM PC
  954. character set.  Each character has an associated attribute that determines
  955. such things as its foreground color, its background color, and whether or not
  956. the character blinks.  In graphics modes, the display screen is divided into
  957. picture elements, or pixels.  Depending on the video mode, the number of
  958. pixel rows ranges between 200 and 480, while the number of columns ranges
  959. between 320 and 720.  Each pixel has an associated value that determines the
  960. color of the pixel.  The number of character cells or pixels available is
  961. called the resolution of the screen.
  962.  
  963.      The display adapter (graphics card) and the video display (monitor)
  964. connected to it determine the video modes available on a given system.  The
  965. following table summarizes the characteristics of the PC and PS/2 video modes
  966. that Fastgraph supports.
  967.  
  968.  Mode                       No. of     Supported          Supported
  969. Number  Type    Resolution  Colors     Adapters           Displays
  970.  
  971.    0    Text    40 x 25     16/8       CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  972.    1    Text    40 x 25     16/8       CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  973.    2    Text    80 x 25     16/8       CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  974.    3    Text    80 x 25     16/8       CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  975.    4  Graphics  320 x 200   4          CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  976.    5  Graphics  320 x 200   4          CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  977.    6  Graphics  640 x 200   2/16       CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  978.    7    Text    80 x 25     b/w        MDA,HGC,EGA,VGA    Monochrome,ECD,VGA
  979.    9  Graphics  320 x 200   16         Tandy 1000,PCjr    RGB
  980.   11  Graphics  720 x 348   b/w        HGC                Monochrome
  981.   12  Graphics  320 x 200   b/w        HGC                Monochrome
  982.   13  Graphics  320 x 200   16         EGA,VGA            RGB,ECD,VGA
  983.   14  Graphics  640 x 200   16         EGA,VGA            RGB,ECD,VGA
  984.   15  Graphics  640 x 350   b/w        EGA,VGA            Monochrome,VGA
  985.   16  Graphics  640 x 350   16/64      EGA,VGA            ECD,VGA
  986.   17  Graphics  640 x 480   2/256K     VGA,MCGA           VGA
  987.   18  Graphics  640 x 480   16/256K    VGA                VGA
  988.   19  Graphics  320 x 200   256/256K   VGA,MCGA           VGA
  989.   20  Graphics  320 x 200   256/256K   VGA                VGA
  990.   21  Graphics  320 x 400   256/256K   VGA                VGA
  991.  
  992.      Some notes about the format and abbreviations used in this table are in
  993. order.  A single value in the "number of colors" column refers to the number
  994. of colors available in that video mode.  In text modes, a pair of numbers
  995. such as 16/8 means each displayed character can have one of 16 foreground
  996.                                       Chapter 2:  PC and PS/2 Video Modes   17
  997.  
  998. colors and one of 8 background colors.  In graphics modes, a pair of numbers
  999. such as 16/64 means 16 colors can be displayed simultaneously from a
  1000. collection, or palette, of 64.  The "b/w" listed in the monochrome modes
  1001. stands for "black and white".  Characters or pixels in these video modes do
  1002. not really have associated colors but instead have display attributes such as
  1003. blinking or different intensities.
  1004.  
  1005.      The meanings of the abbreviations in the "supported adapters" and
  1006. "supported displays" columns are:
  1007.  
  1008.      CGA       Color Graphics Adapter
  1009.      ECD       Enhanced Color Display
  1010.      EGA       Enhanced Graphics Adapter
  1011.      HGC       Hercules Graphics Card
  1012.      MCGA      Multi-Color Graphics Array
  1013.      MDA       Monochrome Display Adapter
  1014.      RGB       Red-Green-Blue Color Display
  1015.      VGA       Video Graphics Array
  1016.  
  1017. The use of the term "VGA" in the "supported display" column refers to any
  1018. analog display, such as a VGA or Multisync monitor.
  1019.  
  1020.      The IBM PS/2 family does not have an adapter and display combination per
  1021. se.  Instead, the video hardware used in these systems is called the video
  1022. subsystem.  The Model 25 and Model 30 have an MCGA-based video subsystem,
  1023. while other models have a VGA-based video subsystem.
  1024.  
  1025.      This rest of this chapter will provide an overview of the most important
  1026. features and restrictions of each video mode.  The first section will discuss
  1027. the text modes, while the following section will discuss the graphics modes.
  1028.  
  1029.  
  1030. Text Modes
  1031.  
  1032.      There are five text video modes in the IBM PC and PS/2 family.  Four of
  1033. these modes (0, 1, 2, and 3) are designed for color displays, while the
  1034. remaining mode (7) is designed for monochrome displays.  All of the text
  1035. modes were introduced with the original IBM PC.
  1036.  
  1037.      In text modes, the screen is divided into character cells.  There are
  1038. two bytes of video memory associated with each character cell -- one byte for
  1039. the character's ASCII value, and another for the character's display
  1040. attribute.  The amount of video memory required to store one screen of
  1041. information (called a video page) is thus
  1042.  
  1043.                     number_of_columns x number_of_rows x 2
  1044.  
  1045. All text modes use 25 rows, so for the 40-column modes (0 and 1) the size of
  1046. a video page is 2,000 bytes, and for the 80-column modes (2, 3, and 7) the
  1047. size of a video page is 4,000 bytes.
  1048.  
  1049.      Mode               No. of   Supported          Supported
  1050.     Number  Resolution  Colors   Adapters           Displays
  1051.  
  1052.        0    40 x 25     16/8     CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  1053.        1    40 x 25     16/8     CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  1054.  
  1055. 18   Fastgraph User's Guide
  1056.  
  1057.        2    80 x 25     16/8     CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  1058.        3    80 x 25     16/8     CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  1059.        7    80 x 25     b/w      MDA,HGC,EGA,VGA    Monochrome,ECD,VGA
  1060.  
  1061.  
  1062.      The remainder of this section will describe the text video modes in more
  1063. detail.
  1064.  
  1065. Mode 0
  1066.  
  1067.      Mode 0 is a 40-column by 25-row color text mode.  It is often referred
  1068. to as a colorless mode since it was designed to be used with composite or
  1069. television monitors (as opposed to RGB monitors).  When used with these types
  1070. of monitors, the available 16 "colors" appear as distinct shades of gray.
  1071. When used with an RGB monitor, mode 0 is identical in all respects to mode 1.
  1072. There are eight pages of video memory available in mode 0; each occupies
  1073. 2,000 bytes.
  1074.  
  1075.      The use of composite or television monitors as PC video displays has
  1076. virtually disappeared today.  As a result, mode 0 is used infrequently.
  1077.  
  1078. Mode 1
  1079.  
  1080.      Mode 1 is a 40-column by 25-row color text mode.  It is supported across
  1081. all video adapter and color display combinations in the PC and PS/2 families.
  1082. Characters displayed in mode 1 have an associated display attribute that
  1083. defines the character's foreground color, its background color, and whether
  1084. or not it blinks.  Sixteen foreground colors and eight background colors are
  1085. available.  There are eight pages of video memory available in mode 1; each
  1086. occupies 2,000 bytes.
  1087.  
  1088. Mode 2
  1089.  
  1090.      Mode 2 is an 80-column by 25-row color text mode.  Like mode 0, it is
  1091. often referred to as a colorless mode since it was designed to be used with
  1092. composite or television monitors (as opposed to RGB monitors).  When used
  1093. with these types of monitors, the available 16 "colors" appear as distinct
  1094. shades of gray.  When used with an RGB monitor, mode 2 is identical in all
  1095. respects to mode 3.  There are four pages of video memory available in mode
  1096. 2; each occupies 4,000 bytes.
  1097.  
  1098.      The use of composite or television monitors as PC video displays has
  1099. virtually disappeared today.  As a result, mode 2 is used infrequently.
  1100.  
  1101. Mode 3
  1102.  
  1103.      Mode 3 is an 80-column by 25-row color text mode.  It is the default
  1104. video mode for systems that use any type of color display.  This mode is
  1105. supported across all video adapter and color display combinations in the PC
  1106. and PS/2 families.  Characters displayed in mode 3 have an associated display
  1107. attribute that defines the character's foreground color, its background
  1108. color, and whether or not it blinks.  Sixteen foreground colors and eight
  1109. background colors are available.  There are four pages of video memory
  1110. available in mode 3; each occupies 4,000 bytes.
  1111.                                       Chapter 2:  PC and PS/2 Video Modes   19
  1112.  
  1113. Mode 7
  1114.  
  1115.      Mode 7 is the 80-column by 25-row monochrome text mode.  It is the
  1116. default video mode for systems that use a monochrome display.  To use this
  1117. mode, you must have a Monochrome Display Adapter (MDA), Hercules Graphics
  1118. Card (HGC), or an Enhanced Graphics Adapter (EGA) connected to a monochrome
  1119. display.  Most VGA display adapters also provide an emulation mode that
  1120. allows you to use mode 7 with analog displays.  Characters displayed in mode
  1121. 7 have an associated display attribute that defines whether the character is
  1122. invisible, normal, bold, underlined, reversed, blinking, or a combination of
  1123. these.  There is a single 4,000-byte page of video memory available in mode
  1124. 7, but Fastgraph allows you to allocate random-access memory (RAM) to provide
  1125. up to 15 virtual video pages in this mode.
  1126.  
  1127.  
  1128. Graphics Modes
  1129.  
  1130.      There are 13 standard graphics video modes available in the IBM PC and
  1131. PS/2 family.  Fastgraph provides support for 11 of the 13 modes (modes 8 and
  1132. 10, which are specific to the PCjr and Tandy 1000 systems, are not
  1133. supported).  In addition to these 13 modes, Fastgraph supports two video
  1134. modes for the Hercules Graphics Card (modes 11 and 12), and two added VGA
  1135. modes (modes 20 and 21).
  1136.  
  1137.      Modes 4, 5, and 6 are designed to be used with the Color Graphics
  1138. Adapter (CGA) and for this reason are called the native CGA modes.  They were
  1139. the only graphics modes available with the original IBM PC.  Newer graphics
  1140. adapters (EGA, VGA, and MCGA) can emulate the CGA, which means that the CGA
  1141. graphics modes are available on any PC or PS/2 system equipped with a color
  1142. display.
  1143.  
  1144.  
  1145.      Mode               No. of   Supported          Supported
  1146.     Number  Resolution  Colors   Adapters           Displays
  1147.  
  1148.        4    320 x 200   4        CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  1149.        5    320 x 200   4        CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  1150.        6    640 x 200   2/16     CGA,EGA,VGA,MCGA   RGB,ECD,VGA
  1151.  
  1152.  
  1153.      Modes 8, 9, and 10 are only available on the PCjr and Tandy 1000 series
  1154. computers (these systems also support modes 4, 5, and 6).  Modes 8 and 10 are
  1155. not widely used, and for this reason Fastgraph does not support them.
  1156.  
  1157.  
  1158.      Mode               No. of   Supported         Supported
  1159.     Number  Resolution  Colors   Adapters          Displays
  1160.  
  1161.        8    160 x 200   16       Tandy 1000,PCjr   RGB
  1162.        9    320 x 200   16       Tandy 1000,PCjr   RGB
  1163.       10    640 x 200   4        Tandy 1000,PCjr   RGB
  1164.  
  1165.      Modes 11 and 12 are used with the Hercules Graphics Card (HGC) and a
  1166. monochrome display.  As such, they are not true IBM video modes, but because
  1167. of the popularity of the HGC, Fastgraph provides support for this adapter.
  1168.  
  1169. 20   Fastgraph User's Guide
  1170.  
  1171. IBM has not defined video modes with numbers 11 and 12.  Should they choose
  1172. to do so in the future, Fastgraph's Hercules video mode numbers would likely
  1173. change.
  1174.  
  1175.  
  1176.      Mode               No. of   Supported   Supported
  1177.     Number  Resolution  Colors   Adapters    Displays
  1178.  
  1179.       11    720 x 348   b/w      HGC         Monochrome
  1180.       12    320 x 200   b/w      HGC         Monochrome
  1181.  
  1182.  
  1183.  
  1184.      Modes 13 through 16 were introduced with the Enhanced Graphics Adapter
  1185. (EGA) and for this reason are called the native EGA modes.  The VGA also
  1186. provides support for these modes, but the MCGA does not.  The original IBM
  1187. EGA only contained 64K bytes of video memory, but memory could be added in
  1188. 64K increments to fully populate the adapter with 256K bytes of video memory.
  1189. As other manufacturers developed EGA cards, they generally included 256K
  1190. bytes of video memory as a standard feature.
  1191.  
  1192.  
  1193.      Mode               No. of   Supported   Supported
  1194.     Number  Resolution  Colors   Adapters    Displays
  1195.  
  1196.       13    320 x 200   16       EGA,VGA     RGB,ECD,VGA
  1197.       14    640 x 200   16       EGA,VGA     RGB,ECD,VGA
  1198.       15    640 x 350   b/w      EGA,VGA     Monochrome,VGA
  1199.       16    640 x 350   16/64    EGA,VGA     ECD,VGA
  1200.  
  1201.      Modes 17, 18, and 19 were introduced with the MCGA and VGA video
  1202. subsystems of the IBM PS/2 computers.  Since the introduction of the PS/2,
  1203. other manufacturers have developed VGA cards that can be used with the PC
  1204. family.  The VGA supports all three of these modes, but the MCGA does not
  1205. support mode 18.
  1206.  
  1207.      Mode 20 is a special VGA version of mode 19, while mode 21 uses scan
  1208. doubling to acheive a 400-line VGA display.  These are not true IBM video
  1209. modes, and should IBM define video modes 20 or 21 in the future, Fastgraph's
  1210. numbers for these video modes would likely change.
  1211.  
  1212.  
  1213.  
  1214.      Mode               No. of     Supported   Supported
  1215.     Number  Resolution  Colors     Adapters    Displays
  1216.  
  1217.       17    640 x 480   2/256K     VGA,MCGA    VGA
  1218.       18    640 x 480   16/256K    VGA         VGA
  1219.       19    320 x 200   256/256K   VGA,MCGA    VGA
  1220.       20    320 x 200   256/256K   VGA         VGA
  1221.       21    320 x 400   256/256K   VGA         VGA
  1222.  
  1223.                                       Chapter 2:  PC and PS/2 Video Modes   21
  1224.  
  1225.      The remainder of this section will describe the graphics video modes in
  1226. more detail.
  1227.  
  1228. Mode 4
  1229.  
  1230.      Mode 4 is a CGA graphics mode with a resolution of 320 horizontal pixels
  1231. by 200 vertical pixels.  Each pixel can assume one of four colors (the
  1232. available colors are determined by which one of six palettes has been
  1233. selected), so each pixel requires two bits of video memory.  This means that
  1234. each byte of video memory represents four pixels.  Hence, the amount of video
  1235. memory required to store one full screen of information is
  1236.  
  1237.           320 rows x 200 columns
  1238.           ---------------------- = 16,000 bytes
  1239.              4 pixels per byte
  1240.  
  1241. which is the amount of video memory on a true CGA.  This means that only a
  1242. single video page is available in mode 4, but Fastgraph allows you to
  1243. allocate random-access memory (RAM) to provide up to 15 virtual video pages
  1244. in this mode.
  1245.  
  1246. Mode 5
  1247.  
  1248.      Mode 5 is the colorless analog of mode 4.  It was designed to be used
  1249. with composite or television monitors (as opposed to RGB monitors).  When
  1250. used with these types of monitors, the four colors appear as distinct shades
  1251. of gray.  When used with an RGB monitor, mode 5 is essentially identical to
  1252. mode 4.
  1253.  
  1254.      The use of composite or television monitors as PC video displays has
  1255. virtually disappeared today.  As a result, mode 5 is used infrequently.
  1256.  
  1257. Mode 6
  1258.  
  1259.      Mode 6 is a CGA graphics mode with a resolution of 640 horizontal pixels
  1260. by 200 vertical pixels.  Each pixel can assume two states -- on or off.  The
  1261. color in which the "on" pixels appear can be selected from a palette of 16
  1262. available colors.  Each pixel thus requires one bit of video memory, which
  1263. means that each byte of video memory represents eight pixels.  Hence, the
  1264. amount of video memory required to store one full screen of information is
  1265.  
  1266.           640 rows x 200 columns
  1267.           ---------------------- = 16,000 bytes
  1268.              8 pixels per byte
  1269.  
  1270. which is the amount of video memory on a true CGA.  This means that only a
  1271. single video page is available in mode 6, but Fastgraph allows you to
  1272. allocate random-access memory (RAM) to provide up to 15 virtual video pages
  1273. in this mode.
  1274.  
  1275. Mode 9
  1276.  
  1277.      Mode 9 is a Tandy 1000 and PCjr graphics mode with a resolution of 320
  1278. horizontal pixels by 200 vertical pixels.  Each pixel can assume one of 16
  1279. colors, so each pixel requires four bits of video memory.  This means that
  1280. each byte of video memory represents two pixels.  Hence, the amount of video
  1281. memory required to store one full screen of information is
  1282. 22   Fastgraph User's Guide
  1283.  
  1284.  
  1285.           320 rows x 200 columns
  1286.           ---------------------- = 32,000 bytes
  1287.              2 pixels per byte
  1288.  
  1289. The Tandy 1000 and PCjr use standard random-access memory (RAM) as video
  1290. memory.  When you select mode 9, the ROM BIOS allocates 32,000 bytes of RAM
  1291. which is then treated as a single page of video memory.  Fastgraph allows you
  1292. to allocate additional RAM to provide up to 15 virtual video pages in this
  1293. mode.
  1294.  
  1295. Mode 11
  1296.  
  1297.      Mode 11 is a true Hercules graphics mode with a resolution of 720
  1298. horizontal pixels by 348 vertical pixels.  Each pixel can assume two states
  1299. -- on or off.  Each pixel thus requires one bit of video memory, which means
  1300. that each byte of video memory represents eight pixels.  Hence, the amount of
  1301. video memory required to store one full screen of information is
  1302.  
  1303.           720 rows x 348 columns
  1304.           ---------------------- = 31,320 bytes
  1305.              8 pixels per byte
  1306.  
  1307. which is just less than half the 64K bytes of video memory on a Hercules
  1308. Graphics Card.  In other words, two video pages are available in mode 11.
  1309. Fastgraph allows you to allocate random-access memory (RAM) to provide up to
  1310. 14 virtual video pages in this mode.
  1311.  
  1312. Mode 12
  1313.  
  1314.      Mode 12 is a software-simulated Hercules graphics mode with an effective
  1315. resolution of 320 horizontal pixels by 200 vertical pixels.  Its purpose is
  1316. to provide a resolution that is available with all of the other display
  1317. adapters.  As in mode 11, two video pages are available, but Fastgraph allows
  1318. you to allocate random-access memory (RAM) to provide up to 14 virtual video
  1319. pages in this mode.
  1320.  
  1321.      This mode converts all coordinates from the 320 by 200 space (called
  1322. virtual coordinates) into the 720 by 348 coordinate system (called physical
  1323. coordinates).  It does this by using two physical pixels for each virtual
  1324. pixel and scan doubling the odd-numbered virtual rows.  Finally, offsets are
  1325. added to the resulting physical coordinates to center the image area on the
  1326. display.  This creates an image area bounded horizontally by the physical
  1327. coordinates 40 and 679 and vertically by the physical coordinates 24 and 323.
  1328.  
  1329. Mode 13
  1330.  
  1331.      Mode 13 is an EGA color graphics mode with a resolution of 320
  1332. horizontal pixels by 200 vertical pixels.  Each pixel can assume one of 16
  1333. colors, so each pixel requires four bits of video memory.  This means that
  1334. each byte of video memory represents two pixels.  Hence, the amount of video
  1335. memory required to store one full screen of information is
  1336.  
  1337.           320 rows x 200 columns
  1338.           ---------------------- = 32,000 bytes
  1339.              2 pixels per byte
  1340.                                       Chapter 2:  PC and PS/2 Video Modes   23
  1341.  
  1342. which is 1/8 the 256K bytes of video memory on a fully-populated EGA.  In
  1343. other words, eight video pages are available in mode 13.
  1344.  
  1345. Mode 14
  1346.  
  1347.      Mode 14 is an EGA color graphics mode with a resolution of 640
  1348. horizontal pixels by 200 vertical pixels.  Each pixel can assume one of 16
  1349. colors, so each pixel requires four bits of video memory.  This means that
  1350. each byte of video memory represents two pixels.  Hence, the amount of video
  1351. memory required to store one full screen of information is
  1352.  
  1353.           640 rows x 200 columns
  1354.           ---------------------- = 64,000 bytes
  1355.              2 pixels per byte
  1356.  
  1357. which is 1/4 the 256K bytes of video memory on a fully-populated EGA.  In
  1358. other words, four video pages are available in mode 14.
  1359.  
  1360. Mode 15
  1361.  
  1362.      Mode 15 is an EGA monochrome graphics mode with a resolution of 640
  1363. horizontal pixels by 350 vertical pixels.  Each pixel can assume one of 4
  1364. display attributes, so each pixel requires two bits of video memory.  This
  1365. means that each byte of video memory represents four pixels.  Hence, the
  1366. amount of video memory required to store one full screen of information is
  1367.  
  1368.           640 rows x 350 columns
  1369.           ---------------------- = 56,000 bytes
  1370.              4 pixels per byte
  1371.  
  1372. which is less than 1/4 the 256K bytes of video memory on a fully-populated
  1373. EGA.  However, mode 15 disables two of the EGA's four bit planes, which
  1374. effectively reduces the EGA's video memory to a maximum of 128K bytes.  In
  1375. other words, two video pages are available in mode 15.
  1376.  
  1377. Mode 16
  1378.  
  1379.      Mode 16 is an EGA color graphics mode with a resolution of 640
  1380. horizontal pixels by 350 vertical pixels.  Each pixel can assume one of 16
  1381. colors (the 16 colors can be selected from a palette of 64 colors), so each
  1382. pixel requires four bits of video memory.  This means that each byte of video
  1383. memory represents two pixels.  Hence, the amount of video memory required to
  1384. store one full screen of information is
  1385.  
  1386.           640 rows x 350 columns
  1387.           ---------------------- = 112,000 bytes
  1388.              2 pixels per byte
  1389.  
  1390. which is less than 1/2 the 256K bytes of video memory on a fully-populated
  1391. EGA.  In other words, two video pages are available in mode 16.
  1392.  
  1393. Mode 17
  1394.  
  1395.      Mode 17 is a VGA and MCGA graphics mode with a resolution of 640
  1396. horizontal pixels by 480 vertical pixels.  Each pixel can assume two states
  1397. -- on or off.  The color in which the "on" and "off" pixels appear can be
  1398. selected from a palette of 262,144 available colors.  Each pixel thus
  1399. 24   Fastgraph User's Guide
  1400.  
  1401. requires one bit of video memory, which means that each byte of video memory
  1402. represents eight pixels.  Hence, the amount of video memory required to store
  1403. one full screen of information is
  1404.  
  1405.           640 rows x 480 columns
  1406.           ---------------------- = 38,400 bytes
  1407.              8 pixels per byte
  1408.  
  1409. which is 60% of the 64K bytes of video memory on a true MCGA.  This means
  1410. that only a single video page is available in mode 17.
  1411.  
  1412. Mode 18
  1413.  
  1414.      Mode 18 is a native VGA color graphics mode with a resolution of 640
  1415. horizontal pixels by 480 vertical pixels.  Each pixel can assume one of 16
  1416. colors (the 16 colors can be selected from a palette of 262,144 colors), so
  1417. each pixel requires four bits of video memory.  This means that each byte of
  1418. video memory represents two pixels.  Hence, the amount of video memory
  1419. required to store one full screen of information is
  1420.  
  1421.           640 rows x 480 columns
  1422.           ---------------------- = 153,600 bytes
  1423.              2 pixels per byte
  1424.  
  1425. which is 60% of the 256K bytes of video memory on a standard VGA.  This means
  1426. that only a single video page is available in mode 18.
  1427.  
  1428. Mode 19
  1429.  
  1430.      Mode 19 is a VGA and MCGA color graphics mode with a resolution of 320
  1431. horizontal pixels by 200 vertical pixels.  Each pixel can assume one of 256
  1432. colors (the 256 colors can be selected from a palette of 262,144 colors), so
  1433. each pixel requires eight bits of video memory.  This means that each byte of
  1434. video memory represents one pixel.  Hence, the amount of video memory
  1435. required to store one full screen of information is
  1436.  
  1437.           320 rows x 200 columns
  1438.           ---------------------- = 64,000 bytes
  1439.              1 pixel per byte
  1440.  
  1441. which is the amount of video memory on a true MCGA.  This means that only a
  1442. single video page is available in mode 19, but Fastgraph allows you to
  1443. allocate random-access memory (RAM) to provide up to 15 virtual video pages
  1444. in this mode.
  1445.  
  1446. Mode 20
  1447.  
  1448.      Mode 20 is a VGA color graphics mode with a resolution of 320 horizontal
  1449. pixels by 200 vertical pixels.  Each pixel can assume one of 256 colors (the
  1450. 256 colors can be selected from a palette of 262,144 colors), so each pixel
  1451. requires eight bits of video memory.  This means that each byte of video
  1452. memory represents one pixel.  Hence, the amount of video memory required to
  1453. store one full screen of information is
  1454.  
  1455.           320 rows x 200 columns
  1456.           ---------------------- = 64,000 bytes
  1457.              1 pixel per byte
  1458.                                       Chapter 2:  PC and PS/2 Video Modes   25
  1459.  
  1460.  
  1461. which is 1/4 the 256K bytes of video memory on a standard VGA.  In other
  1462. words, four video pages are available in mode 20.
  1463.  
  1464. Mode 21
  1465.  
  1466.      Mode 21 is a VGA color graphics mode with a resolution of 320 horizontal
  1467. pixels by 400 vertical pixels.  Each pixel can assume one of 256 colors (the
  1468. 256 colors can be selected from a palette of 262,144 colors), so each pixel
  1469. requires eight bits of video memory.  This means that each byte of video
  1470. memory represents one pixel.  Hence, the amount of video memory required to
  1471. store one full screen of information is
  1472.  
  1473.           320 rows x 400 columns
  1474.           ---------------------- = 128,000 bytes
  1475.              1 pixel per byte
  1476.  
  1477. which is 1/2 the 256K bytes of video memory on a standard VGA.  In other
  1478. words, two video pages are available in mode 21.
  1479. 26   Fastgraph User's Guide
  1480.  
  1481.  
  1482. Chapter 3
  1483.  
  1484. Initializing the Video Environment
  1485. 28   Fastgraph User's Guide
  1486.  
  1487.  
  1488. Overview
  1489.  
  1490.      Before Fastgraph can perform any text or graphics video operations, you
  1491. must select a video mode in which your program will run.  An important part
  1492. of this selection depends on whether your program will run in a text mode, a
  1493. graphics mode, or both.  This chapter discusses the necessary video
  1494. initialization for each case.
  1495.  
  1496.  
  1497. Establishing a Text Mode
  1498.  
  1499.      When you write a program that only uses text modes, you must determine
  1500. if the program will run on monochrome systems, color systems, or both.  In
  1501. general, there is no reason to exclude one type of system, because the
  1502. additional programming required to support both is rather trivial.
  1503.  
  1504.      The Fastgraph routine fg_setmode establishes a video mode and
  1505. initializes all of Fastgraph's internal parameters for that mode.  This
  1506. routine has a single integer argument whose value is a video mode number
  1507. between 0 and 21.  Its value can also be -1, which tells Fastgraph to use the
  1508. current video mode.  Specifying an fg_setmode argument of -1 is often useful
  1509. in programs that only use text video modes.
  1510.  
  1511.      When you establish a text video mode, the ROM BIOS text cursor is made
  1512. visible, and this is often undesirable.  The Fastgraph routine fg_cursor
  1513. controls the visibility of the text cursor.  The fg_cursor routine has a
  1514. single integer argument that specifies the cursor visibility.  If its value
  1515. is 0, the cursor is made invisible; if its value is 1, the cursor is made
  1516. visible.
  1517.  
  1518.      At this point, an example may help to clarify things.  The following
  1519. program shows how to initialize Fastgraph for the 80-column color text mode
  1520. (mode 3) and turn off the text mode cursor.  It uses two Fastgraph routines
  1521. that we have not yet discussed, fg_setcolor and fg_text.  These routines will
  1522. be discussed in later sections of this document.  For now, it should suffice
  1523. to know the call to fg_setcolor makes subsequent text appear in bright white,
  1524. and the call to fg_text displays the characters passed to it.
  1525.  
  1526.                                  Example 3-1.
  1527.  
  1528.                        main()
  1529.                        {
  1530.                           fg_setmode(3);
  1531.                           fg_cursor(0);
  1532.  
  1533.                           fg_setcolor(15);
  1534.                           fg_text("Hello, world.",13);
  1535.                        }
  1536.  
  1537.  
  1538.      If you run example 3-1, notice the text displayed by the program appears
  1539. in the upper left corner of the screen.  On the line below this, the DOS
  1540. prompt appears, waiting for your next DOS command.  Furthermore, if your
  1541. system uses the ANSI.SYS driver to set screen attributes (such as with
  1542. Norton's SA program), you should also notice only the DOS prompt appears in
  1543.                            Chapter 3:  Initializing the Video Environment   29
  1544.  
  1545. the colors defined by the screen attributes -- the rest of the screen is
  1546. blank.
  1547.  
  1548.      A more graceful return to DOS is needed.  In example 3-2, we'll use the
  1549. Fastgraph routine fg_reset.  This routine erases the screen, and if the
  1550. ANSI.SYS driver is loaded, fg_reset also restores any previously set screen
  1551. attributes.  We've also included a call to the Fastgraph routine fg_waitkey
  1552. to wait for a keystroke before exiting.  If we didn't do this, we would never
  1553. see the program's output.
  1554.  
  1555.                                  Example 3-2.
  1556.  
  1557.                        main()
  1558.                        {
  1559.  
  1560.                           fg_setmode(3);
  1561.                           fg_cursor(0);
  1562.  
  1563.                           fg_setcolor(15);
  1564.                           fg_text("Hello, world.",13);
  1565.  
  1566.                           fg_waitkey();
  1567.                           fg_reset();
  1568.                        }
  1569.  
  1570.  
  1571.      Since examples 3-1 and 3-2 specifically used video mode 3, they would
  1572. not work on a monochrome system.  Ideally we would like to use fg_setmode(3)
  1573. for color systems and fg_setmode(7) for monochrome systems.  To do this, we
  1574. need a way to determine whether the program is being run on a color system or
  1575. on a monochrome system.  The next example illustrates an easy way to
  1576. accomplish this.
  1577.  
  1578.      Example 3-3 uses the Fastgraph routine fg_testmode to determine if the
  1579. user's system will support the video mode number specified as its first
  1580. argument (the second argument is the number of video pages required, which
  1581. will be 1 for all of the examples in this section).  The fg_testmode routine
  1582. returns a value of 1 (as its function value) if the requested video mode can
  1583. be used, and it returns 0 if not.  The program first sees if an 80-column
  1584. color text mode is available (mode 3), and if so, it selects that mode.  If
  1585. the color mode is not available, it checks if the monochrome text mode is
  1586. available (mode 7), and if so, it chooses the monochrome mode.  If neither
  1587. mode is available, then the program assumes the user's system has a 40-column
  1588. display, issues a message indicating the program requires an 80-column
  1589. display, and then exits.
  1590.  
  1591.                                  Example 3-3.
  1592.  
  1593.                    main()
  1594.                    {
  1595.                       int old_mode;
  1596.  
  1597.                       old_mode = fg_getmode();
  1598.  
  1599.                       if (fg_testmode(3,1))
  1600.                          fg_setmode(3);
  1601.  
  1602. 30   Fastgraph User's Guide
  1603.  
  1604.                       else if (fg_testmode(7,1))
  1605.                          fg_setmode(7);
  1606.                       else {
  1607.                          printf("This program requires\n");
  1608.                          printf("an 80-column display.\n");
  1609.                          exit();
  1610.                          }
  1611.  
  1612.                       fg_cursor(0);
  1613.  
  1614.                       fg_setcolor(15);
  1615.                       fg_text("Hello, world.",13);
  1616.  
  1617.                       fg_waitkey();
  1618.  
  1619.                       fg_setmode(old_mode);
  1620.                       fg_reset();
  1621.                    }
  1622.  
  1623.  
  1624.      Example 3-3 also illustrates another useful procedure.  It is
  1625. recommended, especially in graphics modes, to restore the original video mode
  1626. and screen attributes before a program returns to DOS.  We've already seen
  1627. how the fg_reset routine restores the screen attributes, but how do we
  1628. restore the original video mode?  The Fastgraph routine fg_getmode returns
  1629. the current video mode as its function value.  If we call fg_getmode before
  1630. calling fg_setmode, we can use the return value from fg_getmode and again
  1631. call fg_setmode before the program exits.
  1632.  
  1633.      You can also use another Fastgraph routine, fg_bestmode, to determine if
  1634. a video mode with a specific resolution is available on the user's system.
  1635. The fg_bestmode routine requires three integer arguments:  a horizontal
  1636. resolution, a vertical resolution, and the number of video pages required.
  1637. As its function value, fg_bestmode returns the video mode number that offers
  1638. the most capabilities for the resolution and number of pages requested.  It
  1639. returns a value of -1 if no available video mode offers the requested
  1640. criteria.
  1641.  
  1642.      For example, if we require an 80 by 25 text mode, we can use the
  1643. function call fg_bestmode(80,25,1) to pick the "best" video mode available
  1644. that offers this capability.  In text modes, the term best means to give
  1645. preference to a color text mode over a monochrome text mode.  Example 3-4
  1646. performs the same function as example 3-3, but it uses fg_bestmode rather
  1647. than fg_testmode.
  1648.  
  1649.                                  Example 3-4.
  1650.  
  1651.                    main()
  1652.                    {
  1653.                       int old_mode;
  1654.                       int new_mode;
  1655.  
  1656.                       old_mode = fg_getmode();
  1657.                       new_mode = fg_bestmode(80,25,1);
  1658.  
  1659.                       if (new_mode < 0) {
  1660.  
  1661.                            Chapter 3:  Initializing the Video Environment   31
  1662.  
  1663.                          printf("This program requires\n");
  1664.                          printf("an 80-column display.\n");
  1665.                          exit();
  1666.                          }
  1667.  
  1668.                       fg_setmode(new_mode);
  1669.                       fg_cursor(0);
  1670.  
  1671.                       fg_setcolor(15);
  1672.                       fg_text("Hello, world.",13);
  1673.  
  1674.                       fg_waitkey();
  1675.  
  1676.                       fg_setmode(old_mode);
  1677.                       fg_reset();
  1678.                    }
  1679.  
  1680.  
  1681.  
  1682. Establishing a Graphics Mode
  1683.  
  1684.      The steps for establishing a graphics mode are similar to establishing a
  1685. text mode.  However, there are more restrictions since some systems may not
  1686. support all of the graphics video modes.  For example, a program could not
  1687. run in mode 13 on a CGA system, nor could a program run in mode 9 on anything
  1688. except a Tandy 1000 or PCjr system.
  1689.  
  1690.      Example 3-5 shows the recommended techniques for writing an EGA-specific
  1691. program.  The program in this example uses mode 16, the 640 x 350 EGA mode
  1692. that requires an Enhanced Color Display (ECD).  It uses the Fastgraph routine
  1693. fg_egacheck to determine if an EGA and ECD are present.  The fg_egacheck
  1694. routine returns a value of 0 if an EGA is not found, or if there is an EGA
  1695. but no ECD.  If an EGA and ECD are found, it returns a positive integer
  1696. indicating the number of 64K-byte increments of video memory on the EGA.
  1697. Since mode 16 requires 112,000 bytes of video memory for a single video page,
  1698. there must be at least 128K bytes of video memory on the EGA to run this
  1699. program.  Hence we must verify that fg_egacheck returns a value of at least
  1700. 2.
  1701.  
  1702.                                  Example 3-5.
  1703.  
  1704.     main()
  1705.     {
  1706.        int mode;
  1707.  
  1708.        if (fg_egacheck() < 2) {
  1709.           printf("This program requires an Enhanced Graphics Adapter\n");
  1710.           printf("(EGA) and an Enhanced Color Display (ECD).\n");
  1711.           exit();
  1712.           }
  1713.  
  1714.        mode = fg_getmode();
  1715.        fg_setmode(16);
  1716.  
  1717.        fg_setcolor(15);
  1718.        fg_text("Hello, world.",13);
  1719.  
  1720. 32   Fastgraph User's Guide
  1721.  
  1722.        fg_waitkey();
  1723.  
  1724.        fg_setmode(mode);
  1725.        fg_reset();
  1726.     }
  1727.  
  1728.  
  1729.  
  1730.      For graphics programs, it may suffice to write a program to run in a
  1731. specific video mode, but it is often more desirable to write a program that
  1732. will run in any of several video modes.  This is especially true for
  1733. commercial products, since they should run on as many different video
  1734. configurations as possible.
  1735.  
  1736.      Fastgraph includes a routine named fg_automode that determines the
  1737. graphics video mode that offers the most functionality for the user's video
  1738. hardware configuration.  For example, the Tandy 1000 series computers support
  1739. all three CGA modes (4, 5, and 6) and the 320 by 200 16-color Tandy 1000 mode
  1740. (9).  Of these modes, mode 9 offers the most features from a graphics
  1741. standpoint, so fg_automode will return a value of 9 when run on a Tandy 1000
  1742. computer.  The following table summarizes the video mode numbers returned by
  1743. fg_automode for given adapter-display combinations.
  1744.  
  1745.  
  1746.                                          display
  1747.                        adapter   mono   RGB   ECD   VGA
  1748.  
  1749.                           MDA       7     0     7     7
  1750.                           HGC      11     0     0    11
  1751.                           CGA       0     4     0     0
  1752.                           EGA      15    13    16     0
  1753.                           VGA      17    17    17    18
  1754.                          MCGA      17    17    17    19
  1755.                         Tandy       7     9     0     0
  1756.                          PCjr       7     9     0     0
  1757.  
  1758.  
  1759.      Example 3-6 shows how to use fg_automode to determine the "best"
  1760. graphics mode for the user's video hardware.  In graphics modes, the term
  1761. best means the highest resolution, followed by the number of available
  1762. colors.  The program displays a message that includes the selected video mode
  1763. number.
  1764.  
  1765.                                  Example 3-6.
  1766.  
  1767.                     main()
  1768.                     {
  1769.                        int old_mode;
  1770.                        int new_mode;
  1771.                        char string[4];
  1772.  
  1773.                        old_mode = fg_getmode();
  1774.                        new_mode = fg_automode();
  1775.                        fg_setmode(new_mode);
  1776.  
  1777.                        fg_setcolor(15);
  1778.  
  1779.                            Chapter 3:  Initializing the Video Environment   33
  1780.  
  1781.                        fg_text("I'm running in mode ",20);
  1782.                        sprintf(string,"%d.",new_mode);
  1783.                        fg_text(string,3);
  1784.  
  1785.                        fg_waitkey();
  1786.  
  1787.                        fg_setmode(old_mode);
  1788.                        fg_reset();
  1789.                     }
  1790.  
  1791.  
  1792.      For simple programs such as example 3-6, different screen resolutions
  1793. may not be an issue.  However, in more complex graphics programs it is often
  1794. desirable to write a program for a fixed screen resolution.  A common
  1795. practice is to develop graphics programs to run in modes 4 (for CGA), 9
  1796. (Tandy 1000 or PCjr), 12 (Hercules), 13 (EGA or VGA), and 19 (MCGA).  The
  1797. reason for selecting these five modes is they all use the same 320 by 200
  1798. resolution and will run on any IBM PC or PS/2 with graphics capabilities.
  1799.  
  1800.      Example 3-7 performs the same function as example 3-6, but it uses the
  1801. fg_bestmode routine instead of fg_automode to restrict the program to 320 by
  1802. 200 graphics modes.  For this resolution, the fg_bestmode routine will first
  1803. check the availability of mode 19, followed by modes 13, 9, 4, and 12.  If
  1804. fg_bestmode determines no 320 by 200 graphics mode is available (indicated by
  1805. a return value of -1), the program prints an informational message and exits.
  1806. Otherwise it selects the video mode fg_bestmode proposes and continues.
  1807.  
  1808.                                  Example 3-7.
  1809.  
  1810.      main()
  1811.      {
  1812.         int old_mode;
  1813.         int new_mode;
  1814.         char string[4];
  1815.  
  1816.         old_mode = fg_getmode();
  1817.         new_mode = fg_bestmode(320,200,1);
  1818.  
  1819.         if (new_mode < 0) {
  1820.            printf("This program requires a 320 by 200 graphics mode.\n");
  1821.            exit();
  1822.            }
  1823.  
  1824.         fg_setmode(new_mode);
  1825.  
  1826.         fg_setcolor(15);
  1827.         fg_text("I'm running in mode ",20);
  1828.         sprintf(string,"%d.",new_mode);
  1829.         fg_text(string,3);
  1830.  
  1831.         fg_waitkey();
  1832.  
  1833.         fg_setmode(old_mode);
  1834.         fg_reset();
  1835.      }
  1836.  
  1837. 34   Fastgraph User's Guide
  1838.  
  1839.  
  1840.      If your program will not support all PC and PS/2 video modes with the
  1841. same resolution (for example, it will run in some but not all of the 320 by
  1842. 200 graphics modes), you may want to consider using the fg_testmode routine
  1843. instead of fg_bestmode to check for available video modes.  You may also want
  1844. to use fg_testmode to change the video mode precedence used by fg_bestmode.
  1845. For example, mode 13 (EGA) is faster than mode 19 (MCGA), so you may want to
  1846. consider giving EGA precedence over MCGA, especially if your program does not
  1847. use more than 16 colors.
  1848.  
  1849.      Example 3-8 is similar to example 3-7, but it will only run in the 320
  1850. by 200 EGA, MCGA, and CGA graphics modes (video modes 13, 19, and 4
  1851. respectively).  The program uses fg_testmode to select its video mode.  Note
  1852. the order of calls to fg_testmode gives EGA precedence over MCGA, and MCGA
  1853. precedence over CGA.
  1854.  
  1855.                                  Example 3-8.
  1856.  
  1857.         main()
  1858.         {
  1859.            int old_mode;
  1860.            char string[4];
  1861.  
  1862.            old_mode = fg_getmode();
  1863.  
  1864.            if (fg_testmode(13,1))
  1865.               fg_setmode(13);
  1866.            else if (fg_testmode(19,1))
  1867.               fg_setmode(19);
  1868.            else if (fg_testmode(4,1))
  1869.               fg_setmode(4);
  1870.            else {
  1871.               printf("This program requires an EGA, MCGA, or CGA.\n");
  1872.               exit();
  1873.               }
  1874.  
  1875.            fg_setcolor(15);
  1876.            fg_text("I'm running in mode ",20);
  1877.            sprintf(string,"%d.",getmode());
  1878.            fg_text(string,3);
  1879.  
  1880.            fg_waitkey();
  1881.  
  1882.            fg_setmode(old_mode);
  1883.            fg_reset();
  1884.         }
  1885.  
  1886.  
  1887.  
  1888. Summary of Video Initialization Routines
  1889.  
  1890.      This section summarizes the functional descriptions of the Fastgraph
  1891. routines presented in this chapter.  More detailed information about these
  1892. routines, including their arguments and return values, may be found in the
  1893. Fastgraph Reference Manual.
  1894.                            Chapter 3:  Initializing the Video Environment   35
  1895.  
  1896.      FG_AUTOMODE determines the graphics video mode that offers the most
  1897. features for the user's display and adapter configuration.  The value it
  1898. returns helps determine a suitable value to pass to the fg_setmode routine.
  1899.  
  1900.      FG_BESTMODE is similar to fg_automode, but it excludes video modes that
  1901. do not offer the specified resolution and video page requirements.
  1902.  
  1903.      FG_CURSOR makes the text mode cursor visible or invisible.  This routine
  1904. has no effect when used in a graphics mode.
  1905.  
  1906.      FG_EGACHECK returns information about the active EGA or VGA adapter and
  1907. display.  It is useful in checking if the adapter has enough memory to run an
  1908. EGA-specific program.
  1909.  
  1910.      FG_GETMODE returns the current video mode.  It is typically one of the
  1911. first Fastgraph routines called in a program.  The value returned by
  1912. fg_getmode can be retained to restore the original video mode when a program
  1913. transfers control back to DOS.
  1914.  
  1915.      FG_RESET is generally the last Fastgraph routine called in a program.
  1916. It only functions in text video modes.  When the ANSI.SYS driver is not
  1917. loaded, fg_reset merely erases the screen.  When ANSI.SYS is loaded, fg_reset
  1918. also restores any previously set screen attributes.
  1919.  
  1920.      FG_SETMODE establishes a video mode and initializes Fastgraph's internal
  1921. parameters for that video mode.  It must be called before any Fastgraph
  1922. routine that performs video output.  A program can call fg_setmode as many
  1923. times as needed to switch between different video modes.
  1924.  
  1925.      FG_TESTMODE determines whether or not a specified video mode (with a
  1926. given number of video pages) is available on the user's system.
  1927. 36   Fastgraph User's Guide
  1928.  
  1929.  
  1930. Chapter 4
  1931.  
  1932. Coordinate Systems
  1933. 38   Fastgraph User's Guide
  1934.  
  1935.  
  1936. Overview
  1937.  
  1938.      Fastgraph uses three coordinate systems to perform text and graphics
  1939. output.  These coordinate systems are character space, screen space, and
  1940. world space.  The world space coordinate system is not available with
  1941. Fastgraph/Light.
  1942.  
  1943.  
  1944. Character Space
  1945.  
  1946.      The coordinate system used for displaying characters is called character
  1947. space.  Fastgraph uses character space for displaying characters in both text
  1948. and graphics video modes.  It can be thought of as a grid of rows and
  1949. columns, with each cell in the grid holding one character.  Each cell is
  1950. identified by its unique (row,column) integer coordinates.  The rows and
  1951. columns are numbered starting at zero; the origin is always the upper left
  1952. corner of the screen.  For example, in the 80-column by 25-row text modes (2,
  1953. 3, and 7), the (row,column) coordinates of the screen corners are shown in
  1954. the following diagram.
  1955.  
  1956.  
  1957.                             (0,0)           (0,79)
  1958.  
  1959.  
  1960.  
  1961.                             (24,0)         (24,79)
  1962.  
  1963.  
  1964. The number of rows and columns depends on the video mode, as shown in the
  1965. following table.  For graphics modes, the table also includes the width and
  1966. height in pixels of a character cell.
  1967.  
  1968.                       Mode  No. of  No. of Char. Char.
  1969.                      Number Columns Rows   Width Height
  1970.  
  1971.                         0     40      25
  1972.                         1     40      25
  1973.                         2     80      25
  1974.                         3     80      25
  1975.                         4     40      25     8     8
  1976.                         5     40      25     8     8
  1977.                         6     80      25     8     8
  1978.                         7     80      25
  1979.                         9     40      25     8     8
  1980.                        11     80      25     9     14
  1981.                        12     40      25     8     8
  1982.                        13     40      25     8     8
  1983.                        14     80      25     8     8
  1984.                        15     80      25     8     14
  1985.                        16     80      25     8     14
  1986.                        17     80      30     8     16
  1987.                        18     80      30     8     16
  1988.                        19     40      25     8     8
  1989.                        20     40      25     8     8
  1990.                        21     40      50     8     8
  1991.  
  1992.                                            Chapter 4:  Coordinate Systems   39
  1993.  
  1994.  
  1995.      Fastgraph includes two routines, fg_getmaxx and fg_getmaxy, that
  1996. respectively return the maximum column and row numbers in text modes.
  1997. Example 4-1 demonstrates these two routines in a text mode.  The program uses
  1998. fg_getmaxx and fg_getmaxy to obtain the maximum column and row numbers in
  1999. mode 3.  It then displays these values (79 and 24).
  2000.  
  2001.                                  Example 4-1.
  2002.  
  2003.                     main()
  2004.                     {
  2005.                        int max_col;
  2006.                        int max_row;
  2007.                        int mode;
  2008.  
  2009.                        mode = fg_getmode();
  2010.                        fg_setmode(3);
  2011.  
  2012.                        max_col = fg_getmaxx();
  2013.                        max_row = fg_getmaxy();
  2014.  
  2015.                        fg_setmode(mode);
  2016.                        fg_reset();
  2017.  
  2018.                        printf("Last col = %d\n",max_col);
  2019.                        printf("Last row = %d\n",max_row);
  2020.                     }
  2021.  
  2022.  
  2023.  
  2024. Screen Space
  2025.  
  2026.      Screen space is one of two available coordinate systems in graphics
  2027. modes.  It uses the physical device coordinates.  Screen space can be thought
  2028. of as a grid of rows and columns, with each unit in the grid holding one
  2029. pixel.  Each pixel is identified by its unique (x,y) integer coordinates.
  2030. The rows and columns are numbered starting at zero; the origin is always the
  2031. upper left corner of the screen.  For example, in the 320 by 200 graphics
  2032. modes, the (x,y) coordinates of the screen corners are shown in the following
  2033. diagram.
  2034.  
  2035.  
  2036.                             (0,0)          (319,0)
  2037.  
  2038.  
  2039.  
  2040.                             (0,199)      (319,199)
  2041.  
  2042.  
  2043.      The Fastgraph routines fg_getmaxx and fg_getmaxy return the maximum x
  2044. and y screen coordinates when used in graphics modes, as shown in example
  2045. 4-2.  The program uses fg_getmaxx and fg_getmaxy to obtain the maximum x and
  2046. y coordinates in the standard CGA four-color graphics mode (mode 4).  It then
  2047. displays these values (319 and 199).
  2048.  
  2049.                                  Example 4-2.
  2050.  
  2051. 40   Fastgraph User's Guide
  2052.                       main()
  2053.                       {
  2054.                          int maxx;
  2055.                          int maxy;
  2056.                          int mode;
  2057.  
  2058.                          mode = fg_getmode();
  2059.                          fg_setmode(4);
  2060.  
  2061.                          maxx = fg_getmaxx();
  2062.                          maxy = fg_getmaxy();
  2063.  
  2064.                          fg_setmode(mode);
  2065.                          fg_reset();
  2066.  
  2067.                          printf("(%d,%d)\n",maxx,maxy);
  2068.                       }
  2069.  
  2070.  
  2071.  
  2072. World Space
  2073.  
  2074.      World space is the other available coordinate system in graphics modes.
  2075. It utilizes user-defined floating point coordinates.  Fastgraph translates
  2076. world space coordinates into physical device coordinates (screen space), and
  2077. because of this it is somewhat slower than using screen space.  World space
  2078. can be thought of as a standard cartesian plane extending from the lower left
  2079. corner of the screen.
  2080.  
  2081.      Any program that uses world space coordinates must first initialize
  2082. Fastgraph's internal world space parameters.  The Fastgraph routine fg_initw
  2083. is provided for this purpose.  The fg_initw routine has no arguments and must
  2084. be called before any other routine that uses world space coordinates.
  2085.  
  2086.      The next step in using world space is to use the Fastgraph routine
  2087. fg_setworld to define the world space coordinates of the screen edges.  The
  2088. fg_setworld routine has four floating-point arguments -- the minimum x
  2089. coordinate (left edge), the maximum x coordinate (right edge), the minimum y
  2090. coordinate (bottom edge), and the maximum y coordinate (top edge).  For
  2091. example, if you define the world space coordinates with the statement
  2092.  
  2093.                        fg_setworld(-10.0,10.0,0.0,2.5);
  2094.  
  2095. the (x,y) coordinates of the screen corners would be defined as shown in the
  2096. following diagram.
  2097.  
  2098.  
  2099.                             (-10.0,2.5) (10.0,2.5)
  2100.  
  2101.  
  2102.  
  2103.                             (-10.0,0.0) (10.0,0.0)
  2104.  
  2105.  
  2106. Fastgraph includes a routine fg_getworld that returns the world space
  2107. extremes as defined in the most recent call to fg_setworld.
  2108.                                            Chapter 4:  Coordinate Systems   41
  2109.  
  2110.      Example 4-3 uses fg_setworld and fg_getworld to illustrate an
  2111. interesting application of world space.  This program calls another routine
  2112. named redraw (not shown) that erases the screen and draws a certain image
  2113. using world space coordinates.  The program draws the image, waits for a
  2114. keystroke, reduces the world space by a factor of two in each direction, and
  2115. then draws the image again.  This produces a zoom effect in which the image
  2116. appears twice as large as it was originally.
  2117.  
  2118.                                  Example 4-3.
  2119.  
  2120.               main()
  2121.               {
  2122.                  int new_mode, old_mode;
  2123.                  double xmin, xmax, ymin, ymax;
  2124.  
  2125.                  old_mode = fg_getmode();
  2126.                  new_mode = fg_automode();
  2127.  
  2128.                  if (new_mode == 0) {
  2129.                     printf("This program requires graphics.\n");
  2130.                     exit();
  2131.                     }
  2132.  
  2133.                  fg_setmode(new_mode);
  2134.                  fg_initw();
  2135.  
  2136.                  fg_setworld(0.0,40.0,0.0,30.0);
  2137.                  redraw();
  2138.                  fg_waitkey();
  2139.  
  2140.                  fg_getworld(&xmin,&xmax,&ymin,&ymax);
  2141.                  fg_setworld(0.0,xmax*0.5,0.0,ymax*0.5);
  2142.                  redraw();
  2143.                  fg_waitkey();
  2144.  
  2145.                  fg_setmode(old_mode);
  2146.                  fg_reset();
  2147.               }
  2148.  
  2149.  
  2150.  
  2151. Conversion Routines
  2152.  
  2153.      It is often necessary to convert coordinates between character space,
  2154. screen space, and world space.  Fastgraph includes eight conversion routines,
  2155. four for x coordinates and four for y coordinates, to perform such
  2156. conversions.  All of these routines return the translated coordinate as the
  2157. function value.
  2158.  
  2159.      The fg_xalpha and fg_yalpha routines convert screen space coordinates to
  2160. character space.  The fg_xalpha routine converts a screen space x coordinate
  2161. to the character space column that contains the coordinate.  Similarly, the
  2162. fg_yalpha routine converts a screen space y coordinate to the character space
  2163. row that contains the coordinate.
  2164.  
  2165.      The fg_xconvert and fg_yconvert routines convert character space
  2166. coordinates to screen space.  The fg_xconvert routine converts a character
  2167. 42   Fastgraph User's Guide
  2168.  
  2169. space column to the screen space coordinate of its leftmost pixel.
  2170. Similarly, the fg_yconvert routine converts a character space row to the
  2171. screen space coordinate of its top (lowest-numbered) pixel.
  2172.  
  2173.      The fg_xscreen and fg_yscreen routines convert world space coordinates
  2174. to screen space.  The fg_xscreen routine translates x coordinates, while the
  2175. fg_yscreen routine translates y coordinates.  Conversely, the fg_xworld and
  2176. fg_yworld routines convert screen space coordinates to world space.  The
  2177. fg_xworld routine translates x coordinates, while the fg_yworld routine
  2178. translates y coordinates.
  2179.  
  2180.  
  2181. Summary of Coordinate Routines
  2182.  
  2183.      This section summarizes the functional descriptions of the Fastgraph
  2184. routines presented in this chapter.  More detailed information about these
  2185. routines, including their arguments and return values, may be found in the
  2186. Fastgraph Reference Manual.
  2187.  
  2188.      FG_GETMAXX returns the maximum x coordinate in screen space when used in
  2189. a graphics mode.  It returns the maximum column number in character space
  2190. when used in a text mode.
  2191.  
  2192.      FG_GETMAXY returns the maximum y coordinate in screen space when used in
  2193. a graphics mode.  It returns the maximum row number in character space when
  2194. used in a text mode.
  2195.  
  2196.      FG_GETWORLD returns the current world space limits, as defined in the
  2197. most recent call to fg_setworld.
  2198.  
  2199.      FG_INITW initializes Fastgraph's internal parameters for world space.
  2200. This routine must be called once, before any other routine that uses world
  2201. coordinates.
  2202.  
  2203.      FG_SETWORLD defines the world space coordinates that correspond to the
  2204. physical edges of the screen.
  2205.  
  2206.      FG_XALPHA and FG_YALPHA convert screen space coordinates to character
  2207. space.
  2208.  
  2209.      FG_XCONVERT and FG_YCONVERT convert character space coordinates to
  2210. screen space.
  2211.  
  2212.      FG_XSCREEN and FG_YSCREEN convert world space coordinates to screen
  2213. space.
  2214.  
  2215.      FG_XWORLD and FG_YWORLD convert screen space coordinates to world space.
  2216.  
  2217.  
  2218. Chapter 5
  2219.  
  2220. The Use of Color
  2221. 44   Fastgraph User's Guide
  2222.  
  2223.  
  2224. Overview
  2225.  
  2226.      The use of color is an important part of any text or graphics
  2227. application.  This chapter explains color as it applies to text and graphics
  2228. modes.  It also describes palettes and video DAC registers for the graphics
  2229. video modes that offer this functionality.  Finally, an explanation of
  2230. Fastgraph's virtual colors is provided.
  2231.  
  2232.  
  2233. Text Modes
  2234.  
  2235.      The term color is not really correct in text modes because each
  2236. character cell has an associated attribute that controls the character's
  2237. appearance in that cell.  The meaning of the attribute differs for color and
  2238. monochrome text modes.
  2239.  
  2240.  
  2241. Color Modes
  2242.  
  2243.      In color text modes (modes 0, 1, 2, and 3), the attribute determines a
  2244. character's foreground color (the color of the character itself), its
  2245. background color (the color of that part of the character cell not covered by
  2246. the character), and whether or not it blinks.  Sixteen foreground colors
  2247. (numbered 0 to 15) are available, but only eight background colors (numbered
  2248. 0 to 7) are available.  The colors assigned to these values are listed in the
  2249. following table.
  2250.  
  2251.  
  2252.                     number  color     number  color
  2253.  
  2254.                        0    black        8    gray
  2255.                        1    blue         9    light blue
  2256.                        2    green       10    light green
  2257.                        3    cyan        11    light cyan
  2258.                        4    red         12    light red
  2259.                        5    magenta     13    light magenta
  2260.                        6    brown       14    yellow
  2261.                        7    white       15    bright white
  2262.  
  2263.  
  2264.  
  2265. At first it may seem the numbers have been arbitrarily assigned to the
  2266. colors.  Upon further inspection, however, it becomes apparent this is not
  2267. the case.  Each of the color numbers is a four bit quantity of the form IRGB,
  2268. with I representing the intensity, R the red component, G the green
  2269. component, and B the blue component.  If the corresponding bit is 1, it means
  2270. the intensity or color component is set.  For example, normal red would be
  2271. represented by the IRGB bit pattern 0100, which is 4 decimal, the color
  2272. number for red.
  2273.  
  2274.      The fg_setattr routine defines the current text attribute.  Once
  2275. fg_setattr is called, Fastgraph displays all subsequent text using that
  2276. attribute.  The first argument of fg_setattr defines the foreground color,
  2277.                                              Chapter 5:  The Use of Color   45
  2278.  
  2279. which must be an integer between 0 and 15.  Its second argument defines the
  2280. background color, which must be between 0 and 7.  Its third argument
  2281. determines if the foreground color blinks (1 means it blinks, 0 means it does
  2282. not).  For example, the statement
  2283.  
  2284.                              fg_setattr(14,1,0);
  2285.  
  2286. specifies subsequent text will be displayed with a yellow foreground (14) on
  2287. a blue background (1) and will not blink (0).
  2288.  
  2289.      Another Fastgraph routine, fg_setcolor, can also be used to define text
  2290. attributes.  The fg_setcolor routine packs the three values passed to
  2291. fg_setattr into a single argument, as shown below.
  2292.  
  2293.  
  2294.                              bits  attribute
  2295.  
  2296.                              0-3   foreground color
  2297.                              4-6   background color
  2298.                               7    blinking
  2299.  
  2300.  
  2301. For example, calling fg_setcolor with an argument of 30 (1E hex) is
  2302. equivalent to calling fg_setattr with arguments of 14, 1, and 0.
  2303.  
  2304.      The Fastgraph routine fg_getcolor returns the current text attribute, as
  2305. defined in the most recent call to fg_setattr or fg_setcolor.  The
  2306. fg_getcolor routine has no arguments and returns the attribute as its
  2307. function value.  The returned value is encoded using the same scheme for
  2308. passing a text attribute to the fg_setcolor routine.
  2309.  
  2310.  
  2311. Monochrome Mode
  2312.  
  2313.      In the monochrome text mode (mode 7), colors are obviously not
  2314. available.  The attribute instead determines whether a character is
  2315. invisible, normal, bold, reversed, or certain combinations of these.  The
  2316. following table shows the values assigned to the available display
  2317. characteristics.
  2318.  
  2319.  
  2320.                      foreground  background  characteristic
  2321.  
  2322.                           0           0      invisible
  2323.                           0           7      reversed
  2324.                           1           0      underlined
  2325.                           7           0      normal
  2326.                           9           0      underlined bold
  2327.                          15           0      bold
  2328.  
  2329. Additionally, you can turn blinking on or off for each of these combinations.
  2330. Any combination of foreground and background values not listed in the above
  2331. table produces a normal display characteristic.
  2332. 46   Fastgraph User's Guide
  2333.  
  2334.  
  2335.      As in the color modes, the Fastgraph routines fg_setattr and fg_setcolor
  2336. define the current text attribute.  For example, the statement
  2337.  
  2338.                               fg_setattr(0,7,1);
  2339.  
  2340. specifies subsequent text will be displayed in reverse video (0,7) and will
  2341. blink (1).  The same attribute could be defined by calling fg_setcolor with
  2342. an argument of 240 (F0 hex).  The fg_getcolor routine is also available and
  2343. works as it does in the color text modes.
  2344.  
  2345.  
  2346. Graphics Modes
  2347.  
  2348.      In graphics modes, each pixel has an associated color value that
  2349. determines the color in which the pixel is displayed.  The number of
  2350. available colors depends on the video mode.  Some of the graphics modes also
  2351. have palette registers or video DAC registers to provide additional color
  2352. capabilities.  The example programs presented in this section show the use of
  2353. color in specific graphics video modes.
  2354.  
  2355.      The following subsections will discuss the use of color in each of the
  2356. graphics video modes.  In these discussions, there will be several references
  2357. to a group of colors called the standard color set.  This is a set of 16
  2358. colors common to many of the graphics video modes (and also to the color text
  2359. modes).  The colors in the standard color set are listed in the following
  2360. table.
  2361.  
  2362.  
  2363.                     number  color     number  color
  2364.  
  2365.                        0    black        8    gray
  2366.                        1    blue         9    light blue
  2367.                        2    green       10    light green
  2368.                        3    cyan        11    light cyan
  2369.                        4    red         12    light red
  2370.                        5    magenta     13    light magenta
  2371.                        6    brown       14    yellow
  2372.                        7    white       15    bright white
  2373.  
  2374.  
  2375.  
  2376.      At this point it is important to understand the difference between the
  2377. terms color number and color value.  Color number refers to the number that
  2378. defines a color in the standard color set (for example, green is color number
  2379. 2).  Color value refers to the actual value of a pixel in video memory, which
  2380. ultimately determines the color in which that pixel is displayed.  The color
  2381. value is sometimes just called the color.
  2382.  
  2383.      In each of the graphics modes, video memory is zeroed when the
  2384. fg_setmode routine is called.  This means all pixels are initially set to
  2385. color value 0, which by default is black.  For this reason, color value 0 is
  2386. often called the background color in graphics video modes.
  2387.                                              Chapter 5:  The Use of Color   47
  2388.  
  2389.      The Fastgraph routine fg_setcolor defines the color in which subsequent
  2390. graphics operations are performed.  This color is called the current color.
  2391. Depending on the video mode, the current color can reference a color value
  2392. (in CGA and Hercules graphics modes), a palette register (in Tandy, EGA, and
  2393. VGA graphics modes), or a video DAC register (in 256-color modes).  The
  2394. fg_setcolor routine takes a single integer argument that specifies the color.
  2395. When fg_setmode is called, it sets the current color to 0.  The Fastgraph
  2396. routine fg_getcolor returns the current color, as defined in the most recent
  2397. call to fg_setcolor.  The fg_getcolor routine has no arguments and returns
  2398. the current color as its function value.
  2399.  
  2400.  
  2401. CGA Color Modes
  2402.  
  2403.      The CGA color modes (modes 4 and 5) have six sets of available colors,
  2404. called palettes, numbered 0 to 5.  Each palette consists of four colors,
  2405. numbered 0 to 3.  In each palette, the background color (color value 0) can
  2406. be selected from the standard color set, but the other 3 colors are fixed.
  2407. The following table shows the fixed colors assigned to each palette.
  2408.  
  2409.  
  2410.                      palette 0       palette 1       palette 2
  2411.  
  2412.            color 1   light green     light cyan      light cyan
  2413.            color 2   light red       light magenta   light red
  2414.            color 3   yellow          bright white    bright white
  2415.  
  2416.                      palette 3       palette 4       palette 5
  2417.  
  2418.            color 1   green           cyan            cyan
  2419.            color 2   red             magenta         red
  2420.            color 3   brown           white           white
  2421.  
  2422.  
  2423. Palette 1, with a black background, is the default palette when you select
  2424. mode 4.  Palette 2, with a black background, is the default palette when you
  2425. select mode 5.
  2426.  
  2427.      The CGA color modes have a border area called the overscan between the
  2428. addressable pixel space and the physical edges of the screen.  The overscan
  2429. area is always displayed in the background color, regardless of which CGA
  2430. palette is used.
  2431.  
  2432.      In CGA color modes, the fg_setcolor routine defines the current color by
  2433. referencing one of the four color values.  The fg_palette routine selects one
  2434. of the six palettes and defines the background color for that palette.  The
  2435. first argument of the fg_palette routine is an integer between 0 and 5 that
  2436. specifies the palette number.  The second argument is an integer between 0
  2437. and 15 that defines the background color, using the color numbers in the
  2438. standard color set.
  2439.  
  2440.      Example 5-1 demonstrates the use of the fg_palette and fg_setcolor
  2441. routines in mode 4.  After establishing the video mode, the program selects
  2442. palette 0 and makes the background color blue (color number 1).  It then
  2443. makes color 3 in palette 0 (yellow) the current color and displays the word
  2444. "Hello".  Finally, it restores the original video mode and screen attributes
  2445. before returning to DOS.
  2446. 48   Fastgraph User's Guide
  2447.  
  2448.  
  2449.                                  Example 5-1.
  2450.  
  2451.                            main()
  2452.                            {
  2453.                               int mode;
  2454.  
  2455.                               mode = fg_getmode();
  2456.                               fg_setmode(4);
  2457.  
  2458.                               fg_palette(0,1);
  2459.                               fg_setcolor(3);
  2460.                               fg_text("Hello",5);
  2461.                               fg_waitkey();
  2462.  
  2463.                               fg_setmode(mode);
  2464.                               fg_reset();
  2465.                            }
  2466.  
  2467.  
  2468. CGA Two-Color Mode
  2469.  
  2470.      The CGA two-color mode (mode 6) has a fixed background color (color
  2471. value 0) and a user-definable foreground color (color value 1).  The
  2472. background color is always black.  The foreground color is bright white by
  2473. default, but it can be changed to any of the colors in the standard color
  2474. set.  It should be mentioned that changing the foreground color works on true
  2475. CGA adapters, but there are very few EGA and VGA adapters that correctly
  2476. implement changing the foreground color in their mode 6 emulation.
  2477.  
  2478.      In mode 6, the fg_setcolor routine defines the current color by
  2479. referencing one of the two color values.  The fg_palette routine defines the
  2480. actual foreground color (that is, the color of pixels whose color value is
  2481. 1).  To be consistent with the other graphics modes, the fg_palette routine
  2482. has two arguments, but the first one is not used.  The second argument is an
  2483. integer between 0 and 15 that defines the foreground color, using the color
  2484. numbers in the standard color set.
  2485.  
  2486.      Example 5-2 demonstrates the use of the fg_palette and fg_setcolor
  2487. routines in mode 6.  After establishing the video mode, the program makes the
  2488. foreground color yellow (color number 14).  It then makes color 1 the current
  2489. color and displays the word "Hello".  Finally, it restores the original video
  2490. mode and screen attributes before returning to DOS.
  2491.  
  2492.                                  Example 5-2.
  2493.  
  2494.                            main()
  2495.                            {
  2496.                               int mode;
  2497.  
  2498.                               mode = fg_getmode();
  2499.                               fg_setmode(6);
  2500.  
  2501.                               fg_palette(0,14);
  2502.                               fg_setcolor(1);
  2503.                               fg_text("Hello",5);
  2504.  
  2505.                                              Chapter 5:  The Use of Color   49
  2506.  
  2507.                               fg_waitkey();
  2508.  
  2509.                               fg_setmode(mode);
  2510.                               fg_reset();
  2511.                            }
  2512.  
  2513.  
  2514.  
  2515. Tandy and PCjr Modes
  2516.  
  2517.      The supported Tandy 1000 or PCjr graphics mode (mode 9) has 16 color
  2518. values, numbered 0 to 15.  Each color value references one of 16 user-
  2519. definable palette registers, often simply called palettes, also numbered 0 to
  2520. 15.  The values assigned to the palette registers determine the colors in
  2521. which pixels are displayed.  For example, if you assign palette register 2
  2522. the value for red, then pixels whose color value is 2 will be red.
  2523.  
  2524.      Each palette can assume one of the 16 colors in the standard color set.
  2525. By default, the values assigned to the 16 palettes correspond to the
  2526. identically numbered colors in the standard color set.  In other words,
  2527. palette 0 is assigned the value for black, palette 1 is assigned the value
  2528. for blue, and so forth.
  2529.  
  2530.      In mode 9, the fg_setcolor routine defines the current color by
  2531. referencing one of the 16 palette registers.  The fg_palette routine defines
  2532. the actual color assigned to a specific palette register.  The first argument
  2533. of the fg_palette routine is an integer between 0 and 15 that specifies the
  2534. palette number.  The second argument is an integer between 0 and 15 that
  2535. defines the palette value (the color assigned to the palette), using the
  2536. color numbers in the standard color set.
  2537.  
  2538.      You can also use the Fastgraph routine fg_setrgb to define the color
  2539. assigned to a specific palette register.  Whereas the fg_palette routine does
  2540. this using a color number from the standard color set, fg_setrgb defines a
  2541. palette register using red, green, and blue color components plus an
  2542. intensity component.  The first argument of the fg_setrgb routine is an
  2543. integer between 0 and 15 that specifies the palette register number.  The
  2544. remaining three arguments are each integers between -1 and 1 that
  2545. respectively specify the red, green, and blue color components for that
  2546. palette register.  The meanings of the color components are:
  2547.  
  2548.      -1 = color bit and intensity bit are set
  2549.       0 = color bit is reset
  2550.       1 = color bit is set
  2551.  
  2552. Since there is only one intensity bit in mode 9 color values, specifying -1
  2553. for any of the RGB color components produces an intense color.  For example,
  2554. the color light cyan is color number 11 in the standard color set, and it is
  2555. produced by combining green and blue and setting the intensity bit.  This
  2556. means any of these four statements
  2557.  
  2558.  
  2559.                             fg_palette(1,11);
  2560.                             fg_setrgb(1,0,-1,1);
  2561.                             fg_setrgb(1,0,1,-1);
  2562.                             fg_setrgb(1,0,-1,-1);
  2563.  
  2564. 50   Fastgraph User's Guide
  2565.  
  2566.  
  2567. could be used to define palette register 1 as light cyan in mode 9.
  2568.  
  2569.      Example 5-3 demonstrates the use of the fg_palette and fg_setcolor
  2570. routines in mode 9.  After establishing the video mode, the program defines
  2571. palette 0 to be blue (1) and palette 1 to be yellow (14).  Note that defining
  2572. palette 0 changes the background color.  It then makes color 1 the current
  2573. color and displays the word "Hello".  After waiting for a keystroke, the
  2574. program changes the color of "Hello" by changing palette 1 to bright white
  2575. (15).  Finally, it restores the original video mode and screen attributes
  2576. before returning to DOS.
  2577.  
  2578.                                  Example 5-3.
  2579.  
  2580.                            main()
  2581.                            {
  2582.                               int mode;
  2583.  
  2584.                               mode = fg_getmode();
  2585.                               fg_setmode(9);
  2586.  
  2587.                               fg_palette(0,1);
  2588.                               fg_palette(1,14);
  2589.  
  2590.                               fg_setcolor(1);
  2591.                               fg_text("Hello",5);
  2592.                               fg_waitkey();
  2593.  
  2594.                               fg_palette(1,15);
  2595.                               fg_waitkey();
  2596.  
  2597.                               fg_setmode(mode);
  2598.                               fg_reset();
  2599.                            }
  2600.  
  2601.  
  2602. Hercules Mode
  2603.  
  2604.      The Hercules graphics mode (mode 11) has a fixed background color (color
  2605. value 0) and a fixed foreground color (color value 1).  The background color
  2606. is always black, and the foreground color is dependent on the monochrome
  2607. display being used (typically, it is green, amber, or white).
  2608.  
  2609.      The fg_setcolor routine defines the current color value by referencing
  2610. one of the two color values.  The fg_palette routine has no effect in mode
  2611. 11.
  2612.  
  2613.      Example 5-4 demonstrates the use of the fg_setcolor routine in mode 11.
  2614. After establishing the video mode, the program makes color 1 the current
  2615. color and displays the word "Hello".  It then restores the original video
  2616. mode and screen attributes before returning to DOS.
  2617.  
  2618.                                  Example 5-4.
  2619.  
  2620.                            main()
  2621.                            {
  2622.  
  2623.                                              Chapter 5:  The Use of Color   51
  2624.  
  2625.                               int mode;
  2626.  
  2627.                               mode = fg_getmode();
  2628.                               fg_setmode(11);
  2629.  
  2630.                               fg_setcolor(1);
  2631.                               fg_text("Hello",5);
  2632.                               fg_waitkey();
  2633.  
  2634.                               fg_setmode(mode);
  2635.                               fg_reset();
  2636.                            }
  2637.  
  2638.  
  2639.  
  2640. Hercules Low-Resolution Mode
  2641.  
  2642.      The Hercules low-resolution graphics mode (mode 12) has four color
  2643. values, numbered 0 to 3.  The background color is always black, colors 1 and
  2644. 2 are half intensity, and color 3 is full intensity.  Colors 1 and 2 both
  2645. produce normal intensity colors, but they do so with different pixel patterns
  2646. -- color 1 turns on the odd-numbered physical pixels, while color 2 turns on
  2647. the even-numbered physical pixels.  The appearance of colors 1 to 3 is
  2648. dependent on the monochrome display being used (typically, it is green,
  2649. amber, or white).
  2650.  
  2651.      The fg_setcolor routine defines the current color value by referencing
  2652. one of the four color values.  The fg_palette routine has no effect in mode
  2653. 12.
  2654.  
  2655.      Example 5-5 demonstrates the use of the fg_setcolor routine in mode 12.
  2656. After establishing the video mode, the program makes color 3 the current
  2657. color and displays the word "Hello".  It then restores the original video
  2658. mode and screen attributes before returning to DOS.
  2659.  
  2660.                                  Example 5-5.
  2661.  
  2662.                            main()
  2663.                            {
  2664.                               int mode;
  2665.  
  2666.                               mode = fg_getmode();
  2667.                               fg_setmode(12);
  2668.  
  2669.                               fg_setcolor(3);
  2670.                               fg_text("Hello",5);
  2671.                               fg_waitkey();
  2672.  
  2673.                               fg_setmode(mode);
  2674.                               fg_reset();
  2675.                            }
  2676.  
  2677. 52   Fastgraph User's Guide
  2678.  
  2679.  
  2680. EGA 200-Line Modes
  2681.  
  2682.      The 200-line EGA graphics modes (modes 13 and 14) have 16 color values,
  2683. numbered 0 to 15.  Each color value references one of 16 user-definable
  2684. palette registers, often simply called palettes, also numbered 0 to 15.  The
  2685. values assigned to the palette registers determine the colors in which pixels
  2686. are displayed.  For example, if you assign palette register 2 the value for
  2687. red, then pixels whose color value is 2 will be red.
  2688.  
  2689.      Each palette can assume one of the 16 colors in the standard color set.
  2690. By default, the values assigned to the 16 palettes correspond to the
  2691. identically numbered colors in the standard color set.  In other words,
  2692. palette 0 is assigned the value for black, palette 1 is assigned the value
  2693. for blue, and so forth.
  2694.  
  2695.      In modes 13 and 14, the fg_setcolor routine defines the current color by
  2696. referencing one of 16 available palette registers.  The fg_palette routine
  2697. defines the actual color assigned to a specific palette register.  The first
  2698. argument of the fg_palette routine is an integer between 0 and 15 that
  2699. specifies the palette number.  The second argument is an integer that defines
  2700. the palette value (the color assigned to the palette).  Although the actual
  2701. colors are taken from the standard color set, the binary structure of a
  2702. palette value is different from the IRGB format used in the standard color
  2703. set.  In modes 13 and 14, the binary structure of a palette value is IxRGB;
  2704. bit 3 is ignored.  The mode 13 and mode 14 palette values that correspond to
  2705. the standard color set are thus:
  2706.  
  2707.  
  2708.                     value  color      value  color
  2709.  
  2710.                       0    black       16    gray
  2711.                       1    blue        17    light blue
  2712.                       2    green       18    light green
  2713.                       3    cyan        19    light cyan
  2714.                       4    red         20    light red
  2715.                       5    magenta     21    light magenta
  2716.                       6    brown       22    yellow
  2717.                       7    white       23    bright white
  2718.  
  2719.  
  2720.  
  2721.      You can also use the Fastgraph routine fg_setrgb to define the color
  2722. assigned to a specific palette register.  Whereas the fg_palette routine does
  2723. this using a color number from the standard color set, fg_setrgb defines a
  2724. palette register using red, green, and blue color components, plus an
  2725. intensity component.  The first argument of the fg_setrgb routine is an
  2726. integer between 0 and 15 that specifies the palette register number.  The
  2727. remaining three arguments are each integers between -1 and 1 that
  2728. respectively specify the red, green, and blue color components for that
  2729. palette register.  The meanings of the color components are:
  2730.                                              Chapter 5:  The Use of Color   53
  2731.  
  2732.      -1 = color bit and intensity bit are set
  2733.       0 = color bit is reset
  2734.       1 = color bit is set
  2735.  
  2736. Since there is only one intensity bit in mode 13 and 14 color values,
  2737. specifying -1 for any of the RGB color components produces an intense color.
  2738. For example, the color light cyan is represented by the color value 19, and
  2739. it is produced by combining green and blue and setting the intensity bit.
  2740. This means any of these four statements
  2741.  
  2742.                             fg_palette(1,19);
  2743.                             fg_setrgb(1,0,-1,1);
  2744.                             fg_setrgb(1,0,1,-1);
  2745.                             fg_setrgb(1,0,-1,-1);
  2746.  
  2747. could be used to define palette register 1 as light cyan in modes 13 and 14.
  2748.  
  2749.      The Fastgraph routine fg_setcolor defines the color value (that is, the
  2750. palette number) in which subsequent graphics operations are performed.  The
  2751. fg_setcolor routine takes a single integer argument that specifies this
  2752. color.  When fg_setmode is called, it sets the color value to 0.  The
  2753. Fastgraph routine fg_getcolor returns the current color value, as defined in
  2754. the most recent call to fg_setcolor.  The fg_getcolor routine has no
  2755. arguments and returns the current color as the function value.
  2756.  
  2757.      Example 5-6 demonstrates the use of the fg_palette and fg_setcolor
  2758. routines in mode 13.  After establishing the video mode, the program defines
  2759. palette 0 to be blue (1) and palette 1 to be yellow (22).  Note that defining
  2760. palette 0 changes the background color.  It then makes color 1 the current
  2761. color and displays the word "Hello".  After waiting for a keystroke, the
  2762. program changes the color of "Hello" by changing palette 1 to bright white
  2763. (23).  Finally, it restores the original video mode and screen attributes
  2764. before returning to DOS.
  2765.  
  2766.                                  Example 5-6.
  2767.  
  2768.                            main()
  2769.                            {
  2770.                               int mode;
  2771.  
  2772.                               mode = fg_getmode();
  2773.                               fg_setmode(13);
  2774.  
  2775.                               fg_palette(0,1);
  2776.                               fg_palette(1,22);
  2777.  
  2778.                               fg_setcolor(1);
  2779.                               fg_text("Hello",5);
  2780.                               fg_waitkey();
  2781.  
  2782.                               fg_palette(1,23);
  2783.                               fg_waitkey();
  2784.  
  2785.                               fg_setmode(mode);
  2786.                               fg_reset();
  2787.                            }
  2788.  
  2789. 54   Fastgraph User's Guide
  2790.  
  2791. EGA Monochrome Mode
  2792.  
  2793.      The EGA monochrome graphics mode (mode 15) assigns display attributes to
  2794. its four color values, numbered 0 to 3.  Each color value references one of
  2795. four user-definable palette registers, often simply called palettes, numbered
  2796. 0, 1, 4, and 5.  This numbering scheme might seem rather strange at first,
  2797. but it results from the disabling of two of the four video memory bit planes
  2798. in mode 15.  The values assigned to the palette registers determine the pixel
  2799. display attribute.  For example, if you assign palette register 1 the value
  2800. for bold, then pixels whose value is 1 will be bold.
  2801.  
  2802.      In mode 15, the fg_setcolor routine defines the current color (actually,
  2803. a display attribute) by referencing one of the four palette registers.  The
  2804. fg_palette routine defines the actual display attribute assigned to a
  2805. specific palette register.  The first argument of the fg_palette routine is
  2806. an integer that specifies the palette number.  The second argument is an
  2807. integer that defines the palette value (the display attribute assigned to the
  2808. palette).  For each palette register, the following table shows the default
  2809. palette value and its associated display attribute.
  2810.  
  2811.  
  2812.                           palette  palette   display
  2813.                           number    value   attribute
  2814.  
  2815.                              0        0     invisible
  2816.                              1        8     normal
  2817.                              4       24     bold
  2818.                              5       24     bold
  2819.  
  2820.  
  2821.      Example 5-7 demonstrates the use of the fg_palette and fg_setcolor
  2822. routines in mode 15.  After establishing the video mode, the program makes
  2823. color 4 (actually, palette 4, which is bold by default) the current color and
  2824. displays the word "Hello".  After waiting for a keystroke, the program
  2825. changes the display attribute of "Hello" by changing palette 4 to normal
  2826. intensity (palette value 8).  Finally, it restores the original video mode
  2827. and screen attributes before returning to DOS.
  2828.  
  2829.                                  Example 5-7.
  2830.  
  2831.                            main()
  2832.                            {
  2833.                               int mode;
  2834.  
  2835.                               mode = fg_getmode();
  2836.                               fg_setmode(15);
  2837.  
  2838.                               fg_setcolor(4);
  2839.                               fg_text("Hello",5);
  2840.                               fg_waitkey();
  2841.  
  2842.                               fg_palette(4,8);
  2843.  
  2844.                                              Chapter 5:  The Use of Color   55
  2845.  
  2846.                               fg_waitkey();
  2847.  
  2848.                               fg_setmode(mode);
  2849.                               fg_reset();
  2850.                            }
  2851.  
  2852.  
  2853.  
  2854. EGA Enhanced Mode
  2855.  
  2856.      The EGA enhanced graphics mode (mode 16) has 16 color values, numbered 0
  2857. to 15.  Each color value references one of 16 user-definable palette
  2858. registers, often simply called palettes, also numbered 0 to 15.  The values
  2859. assigned to the palette registers determine the colors in which pixels are
  2860. displayed.  For example, if you assign palette register 2 the value for red,
  2861. then pixels whose color value is 2 will be red.
  2862.  
  2863.      Each palette can assume one of 64 available colors.  By default, the
  2864. values assigned to the 16 palettes correspond to the identically numbered
  2865. colors in the standard color set.  In other words, palette 0 is assigned the
  2866. value for black, palette 1 is assigned the value for blue, and so forth.
  2867. There are a few EGA-compatible adapters that do not properly assign the
  2868. default colors to the 16 palette registers, so it is a good practice to do
  2869. this explicitly in mode 16.
  2870.  
  2871.      In mode 16, the fg_setcolor routine defines the current color value by
  2872. referencing one of the 16 palette registers.  The fg_palette routine defines
  2873. the actual color assigned to a specific palette register.  The first argument
  2874. of the fg_palette routine is an integer between 0 and 15 that specifies the
  2875. palette number.  The second argument is an integer that defines the palette
  2876. value (the color assigned to the palette).  The binary structure of a palette
  2877. value is different from the IRGB format used in the standard color set.  In
  2878. mode 16, the binary structure of a palette value is a 6-bit quantity of the
  2879. form rgbRGB, where the lower case letters represent the low intensity (1/3
  2880. intensity) color components, and the upper case letters represent the normal
  2881. intensity (2/3 intensity) color components.  The mode 16 palette values that
  2882. correspond to the standard color set are:
  2883.  
  2884.  
  2885.                     value  color      value  color
  2886.  
  2887.                       0    black        56   gray
  2888.                       1    blue         57   light blue
  2889.                       2    green        58   light green
  2890.                       3    cyan         59   light cyan
  2891.                       4    red          60   light red
  2892.                       5    magenta      61   light magenta
  2893.                      20    brown        62   yellow
  2894.                       7    white        63   bright white
  2895.  
  2896.  
  2897.  
  2898.      The normal intensity components in mode 16 produce the same normal
  2899. intensity colors as in other 16-color graphics modes.  Similarly, combining
  2900. 56   Fastgraph User's Guide
  2901.  
  2902. the low and normal intensities in mode 16 produces the high intensity colors
  2903. of the other modes.  The only exception to this is for the default brown,
  2904. which is formed from the bit pattern 010100 (20 decimal).  This value
  2905. produces a more true brown than the value 6 decimal, which is really an olive
  2906. green.
  2907.  
  2908.      The palette values used in mode 16 are 6-bit quantities, which means
  2909. there are 64 different colors available in mode 16.  This group of 64 colors
  2910. consists of the 16 colors in the standard color set plus 48 additional colors
  2911. that are not available in any of the other EGA modes.  However, because the
  2912. EGA palette registers hold 4-bit quantities, only 16 of these colors can be
  2913. displayed at the same time.  In other words, the EGA enhanced mode provides
  2914. the capability of displaying 16 simultaneous colors from a group of 64.
  2915.  
  2916.      You can also use the Fastgraph routine fg_setrgb to define the color
  2917. assigned to a specific palette register.  Whereas the fg_palette routine does
  2918. this using a value between 0 and 63, fg_setrgb defines a palette register
  2919. using red, green, and blue color components.  The first argument of the
  2920. fg_setrgb routine is an integer between 0 and 15 that specifies the palette
  2921. register number.  The remaining three arguments are each integers between 0
  2922. and 3 that respectively specify the intensities in thirds of the red, green,
  2923. and blue color components for that palette register.  For example, the color
  2924. cyan is represented by the value 3 in the above table, and it is produced by
  2925. combining normal intensity (2/3 intensity) green and blue.  This means either
  2926. of the statements
  2927.  
  2928.                              fg_palette(1,3);
  2929.                              fg_setrgb(1,0,2,2);
  2930.  
  2931. could be used to define palette register 1 as cyan.
  2932.  
  2933.      Example 5-8 demonstrates the use of the fg_palette and fg_setcolor
  2934. routines in mode 16.  It uses the Fastgraph routine fg_rect (discussed in the
  2935. next chapter) to draw rectangles of a specified size.  After establishing the
  2936. video mode, the program uses a for loop to draw 16 equal-size rectangles, one
  2937. in each of the 16 color values.  In the same loop, the program uses the
  2938. fg_palette routine to change each palette to black.  The while loop that
  2939. follows performs four iterations.  The first iteration changes palette 0 to
  2940. 0, palette 1 to 1, and so forth.  Hence, the 16 rectangles appear in the
  2941. palette values 0 to 15.  The rectangles remain in these colors until is key
  2942. is pressed to begin the next iteration.  The second iteration changes palette
  2943. 0 to 16, palette 1 to 17, and so forth.  This makes the 16 rectangles appear
  2944. in the palette values 16 to 31.  Iterations three and four are similar, so
  2945. the overall effect of the program is to display all 64 colors, 16 at a time.
  2946. Finally, the program restores the original video mode and screen attributes
  2947. before returning to DOS.
  2948.  
  2949.                                  Example 5-8.
  2950.  
  2951.                 #define COLORS 16
  2952.                 #define WIDTH  40
  2953.  
  2954.                 main()
  2955.                 {
  2956.                    int base;
  2957.  
  2958.                                              Chapter 5:  The Use of Color   57
  2959.  
  2960.                    int color;
  2961.                    int minx, maxx;
  2962.                    int mode;
  2963.  
  2964.                    mode = fg_getmode();
  2965.                    fg_setmode(16);
  2966.  
  2967.                    base = 0;
  2968.                    minx = 0;
  2969.                    maxx = WIDTH - 1;
  2970.  
  2971.                    for (color = 0; color < COLORS; color++) {
  2972.                       fg_palette(color,0);
  2973.                       fg_setcolor(color);
  2974.                       fg_rect(minx,maxx,0,349);
  2975.                       minx = maxx + 1;
  2976.                       maxx = maxx + WIDTH;
  2977.                       }
  2978.  
  2979.                    while (base < COLORS*4) {
  2980.                       for (color = 0; color < COLORS; color++)
  2981.                          fg_palette(color,base+color);
  2982.                       base += COLORS;
  2983.                       fg_waitkey();
  2984.                       }
  2985.  
  2986.                    fg_setmode(mode);
  2987.                    fg_reset();
  2988.                 }
  2989.  
  2990.  
  2991.  
  2992. VGA and MCGA Two-Color Mode
  2993.  
  2994.      The VGA and MCGA high-resolution two-color mode (mode 17) has a
  2995. background color (color value 0) and a foreground color (color value 1).
  2996. Each color value references one of two user-definable palette registers,
  2997. often simply called palettes, also numbered 0 and 1.  Each palette register
  2998. in turn references one of 16 user-definable 18-bit video DAC registers,
  2999. numbered 0 to 15.  The values assigned to the palette registers and video DAC
  3000. registers determine the colors in which pixels are displayed.  For example,
  3001. if palette register 1 contains the value 3, and video DAC register 3 contains
  3002. the color value for red, then pixels whose color value is 1 (that is, the
  3003. foreground pixels) will be red.
  3004.  
  3005.      By default, palette register 0 references video DAC register 0, and
  3006. palette register 1 references video DAC register 1.  In addition, video DAC
  3007. register 0 initially contains the color value for black, while the other 15
  3008. video DAC registers (1 through 15) contain the color value for bright white.
  3009. This means background pixels (color value 0) are black by default, while
  3010. foreground pixels (color value 1) are bright white.
  3011.  
  3012.      The 18-bit video DAC values consist of three 6-bit red, green, and blue
  3013. color components.  Hence, each color component is an integer between 0 and
  3014. 63; increasing values produce more intense colors.  The default color
  3015. components for DAC register 0 are red=0, blue=0, and green=0, which produces
  3016. black.  The default values for the other DAC registers are red=63, blue=63,
  3017. 58   Fastgraph User's Guide
  3018.  
  3019. and green=63, which produces bright white.  Because the video DAC registers
  3020. are 18 bits long, each DAC can specify one of 262,144 (218) colors.  However,
  3021. because the palette registers hold 1-bit quantities, only two of these colors
  3022. can be displayed at the same time.  In other words, mode 17 provides the
  3023. capability of displaying two simultaneous colors from a group of 262,144.
  3024.  
  3025.      In mode 17, the fg_setcolor routine defines the current color by
  3026. referencing one of the two palette registers.  The fg_palette routine defines
  3027. the value of a palette register by referencing one of the 16 video DAC
  3028. registers.  That is, the fg_palette routine specifies the video DAC register
  3029. that a palette register references.  The first argument of the fg_palette
  3030. routine is either 0 or 1 and specifies the palette number.  The second
  3031. argument is an integer between 0 and 15 that specifies the video DAC register
  3032. for that palette.
  3033.  
  3034.      The Fastgraph routine fg_setrgb defines the value of a video DAC
  3035. register in mode 17.  The first argument of the fg_setrgb routine is an
  3036. integer between 0 and 15 that specifies the DAC register number.  The
  3037. remaining three arguments are each integers between 0 and 63 that
  3038. respectively specify the red, green, and blue color components for that DAC
  3039. register.
  3040.  
  3041.      Example 5-9 demonstrates the use of the fg_palette, fg_setrgb, and
  3042. fg_setcolor routines in mode 17.  After establishing the video mode, the
  3043. program defines DAC register 0 to be blue (red=0, green=0, blue=42) and DAC
  3044. register 1 to be yellow (red=63, green=63, blue=21).  Note that defining DAC
  3045. register 0 changes the background color because palette 0 references DAC
  3046. register 0.  The program then makes color 1 the current color (palette 1
  3047. still references DAC register 1) and displays the word "Hello" in yellow.
  3048. After waiting for a keystroke, the program changes the color of "Hello" by
  3049. making palette 1 reference DAC register 15 (which still contains its default
  3050. value, bright white).  Finally, it restores the original video mode and
  3051. screen attributes before returning to DOS.
  3052.  
  3053.                                  Example 5-9.
  3054.  
  3055.                           main()
  3056.                           {
  3057.                              int mode;
  3058.  
  3059.                              mode = fg_getmode();
  3060.                              fg_setmode(17);
  3061.  
  3062.                              fg_setrgb(0,0,0,42);
  3063.                              fg_setrgb(1,63,63,21);
  3064.  
  3065.                              fg_setcolor(1);
  3066.                              fg_text("Hello",5);
  3067.                              fg_waitkey();
  3068.  
  3069.                              fg_palette(1,15);
  3070.                              fg_waitkey();
  3071.  
  3072.                              fg_setmode(mode);
  3073.                              fg_reset();
  3074.                           }
  3075.  
  3076.                                              Chapter 5:  The Use of Color   59
  3077.  
  3078.  
  3079. VGA 16-Color Mode
  3080.  
  3081.      The VGA high-resolution two-color mode (mode 18) has 16 color values,
  3082. numbered 0 to 15.  Each color value references one of 16 user-definable
  3083. palette registers, often simply called palettes, also numbered 0 to 15.  Each
  3084. palette register in turn references one of 16 user-definable 18-bit video DAC
  3085. registers, likewise numbered 0 to 15.  The values assigned to the palette
  3086. registers and video DAC registers determine the colors in which pixels are
  3087. displayed.  For example, if palette register 1 contains the value 3, and
  3088. video DAC register 3 contains the color value for red, then pixels whose
  3089. color value is 1 will be red.
  3090.  
  3091.      By default, each of the 16 palette registers references the video DAC
  3092. register of the same number.  In addition, the 16 video DAC registers
  3093. respectively contain the color values for the 16 colors in the standard color
  3094. set.
  3095.  
  3096.      The 18-bit video DAC values consist of three 6-bit red, green, and blue
  3097. color components.  Hence, each color component is an integer between 0 and
  3098. 63; increasing values produce more intense colors.  The default RGB color
  3099. components for the 16 video DAC registers are:
  3100.  
  3101.  
  3102.              DAC  R  G  B  color      DAC  R  G  B  color
  3103.  
  3104.               0   0   0 0  black       8   21 21 21 gray
  3105.               1   0   0 42 blue        9   21 21 63 light blue
  3106.               2   0  42 0  green      10   21 63 21 light green
  3107.               3   0  42 42 cyan       11   21 63 63 light cyan
  3108.               4   42  0 0  red        12   63 21 21 light red
  3109.               5   42  0 42 magenta    13   63 21 63 light magenta
  3110.               6   42 21 0  brown      14   63 63 21 yellow
  3111.               7   42 42 42 white      15   63 63 63 bright white
  3112.  
  3113.  
  3114.  
  3115. Because the video DAC registers are 18 bits long, each DAC can specify one of
  3116. 262,144 (218) colors.  However, because the palette registers hold 4-bit
  3117. quantities, only 16 of these colors can be displayed at the same time.  In
  3118. other words, mode 18 provides the capability of displaying 16 simultaneous
  3119. colors from a group of 262,144.
  3120.  
  3121.      In mode 18, the fg_setcolor, fg_palette, and fg_setrgb routines function
  3122. exactly as in mode 17 with one exception:  there are 16 palette registers
  3123. instead of just two.  Example 5-9 on page 58 demonstrates the use of these
  3124. routines in mode 17, but it would also work in mode 18 if that video mode
  3125. number were specified in the call to fg_setmode.
  3126. 60   Fastgraph User's Guide
  3127.  
  3128.  
  3129. VGA and MCGA 256-Color Modes
  3130.  
  3131.      The VGA and MCGA 256-color modes (modes 19, 20, and 21) have 256 color
  3132. values, numbered 0 to 255.  Each color value directly references one of 256
  3133. user-definable 18-bit video DAC registers, also numbered 0 to 255.  The
  3134. values assigned to the video DAC registers determine the colors in which
  3135. pixels are displayed.  For example, if video DAC register 3 contains the
  3136. color value for red, then pixels whose color value is 3 will be red.
  3137.  
  3138.      By default, the first 16 video DAC registers (0 to 15) contain the color
  3139. values for the standard color set.  The next 16 DAC registers (16 to 31)
  3140. contain the color values for a gray scale of gradually increasing intensity.
  3141. The next 216 DAC registers (32 to 247) contain three groups of 72 colors
  3142. each, with the first group (32 to 103) at high intensity, the second group
  3143. (104 to 175) at moderate intensity, and the third group (176 to 247) at low
  3144. intensity.  Each group consists of three ranges of decreasing saturation
  3145. (increasing whiteness), with each range varying in hue from blue to red to
  3146. green.  Finally, the last 8 DAC registers (248 to 255) alternate between
  3147. black and bright white.  This information is summarized in the following
  3148. table.
  3149.  
  3150.  
  3151.             DACs       default color values
  3152.  
  3153.             0 to 15    standard color set
  3154.             16 to 31   gray scale of gradually increasing intensity
  3155.             32 to 55   high saturation, high intensity colors
  3156.             56 to 79   moderate saturation, high intensity colors
  3157.             80 to 103  low saturation, high intensity colors
  3158.             104 to 127 high saturation, moderate intensity colors
  3159.             128 to 151 moderate saturation, moderate intensity colors
  3160.             152 to 175 low saturation, moderate intensity colors
  3161.             176 to 199 high saturation, low intensity colors
  3162.             200 to 223 moderate saturation, low intensity colors
  3163.             224 to 247 low saturation, low intensity colors
  3164.             248 to 255 alternate between black and bright white
  3165.  
  3166.  
  3167.      The 18-bit video DAC values consist of three 6-bit red, green, and blue
  3168. color components.  Hence, each color component is an integer between 0 and
  3169. 63; increasing values produce more intense colors.  Because the video DAC
  3170. registers are 18 bits long, each DAC can specify one of 262,144 (218) colors.
  3171. However, because the color values are 8-bit quantities, only 256 of these
  3172. colors can be displayed at the same time.  In other words, modes 19, 20, and
  3173. 21 provide the capability of displaying 256 simultaneous colors from a group
  3174. of 262,144.
  3175.                                              Chapter 5:  The Use of Color   61
  3176.  
  3177.  
  3178.      In the VGA and MCGA 256-color video modes, the fg_setcolor routine
  3179. defines the current color by referencing on of the 256 video DAC registers.
  3180. The fg_setrgb routine defines the actual color of a video DAC register.  The
  3181. first argument of the fg_setrgb routine is an integer between 0 and 15 that
  3182. specifies the DAC register number.  The remaining three arguments are each
  3183. integers between 0 and 63 that respectively specify the red, green, and blue
  3184. color components for that DAC register.  Another Fastgraph routine,
  3185. fg_getrgb, returns the color components for a specified DAC register.  Its
  3186. arguments are the same as for fg_setrgb, except the last three arguments (the
  3187. return values) are passed by reference rather than by value.
  3188.  
  3189.      You can also the Fastgraph routine fg_palette to define the value of a
  3190. video DAC register in modes 19, 20, and 21.  The first argument of the
  3191. fg_palette routine is an integer between 0 and 255 that specifies the DAC
  3192. register number.  The second argument is an integer between 0 and 63 that
  3193. specifies the color value for that video DAC register, using the same 64
  3194. values as in the EGA enhanced mode (mode 16).
  3195.  
  3196.      Example 5-10 demonstrates the use of the fg_setcolor routine in mode 19.
  3197. The program uses the Fastgraph routine fg_rect to draw vertical lines.  After
  3198. establishing the video mode, the program uses a for loop to draw 256 vertical
  3199. lines, one in each of the 256 colors (using the default DAC values).
  3200. Finally, the program restores the original video mode and screen attributes
  3201. before returning to DOS.
  3202.  
  3203.                                 Example 5-10.
  3204.  
  3205.                #define COLORS 256
  3206.  
  3207.                main()
  3208.                {
  3209.                   int base;
  3210.                   int color;
  3211.                   int mode;
  3212.                   int x;
  3213.  
  3214.                   mode = fg_getmode();
  3215.                   fg_setmode(19);
  3216.  
  3217.                   x = 32;
  3218.  
  3219.                   for (color = 0; color < COLORS; color++) {
  3220.                      fg_setcolor(color);
  3221.                      fg_rect(x,x,0,199);
  3222.                      x++;
  3223.                      }
  3224.                   fg_waitkey();
  3225.  
  3226.                   fg_setmode(mode);
  3227.                   fg_reset();
  3228.                }
  3229.  
  3230. 62   Fastgraph User's Guide
  3231.                                 Example 5-11.
  3232.  
  3233.                    main()
  3234.                    {
  3235.                       int old_mode;
  3236.                       int red, green, blue;
  3237.  
  3238.                       old_mode = fg_getmode();
  3239.                       fg_setmode(19);
  3240.  
  3241.                       fg_setcolor(103);
  3242.                       fg_text("Hello",5);
  3243.                       fg_waitfor(18);
  3244.  
  3245.                       fg_getrgb(103,&red,&green,&blue);
  3246.  
  3247.                       while (red+green+blue > 0) {
  3248.                           if (red > 0) red--;
  3249.                           if (green > 0) green--;
  3250.                           if (blue > 0) blue--;
  3251.                           fg_setrgb(103,red,green,blue);
  3252.                           fg_waitfor(1);
  3253.                           }
  3254.  
  3255.                       fg_setmode(old_mode);
  3256.                       fg_reset();
  3257.                    }
  3258.  
  3259.      Example 5-11 shows an interesting effect available in VGA and MCGA
  3260. modes.  The program uses the Fastgraph routine fg_waitfor (discussed in
  3261. chapter 14) to delay the program's execution.  After establishing the video
  3262. mode, the program displays the word "Hello" in color 103, which by default is
  3263. a pastel blue.  It then uses the Fastgraph routine fg_getrgb to retrieve the
  3264. color components for this color.  The while loop gradually decreases the
  3265. color components until all three components are zero, which makes the word
  3266. "Hello" smoothly fade to black.  Finally, the program restores the original
  3267. video mode and screen attributes before returning to DOS.
  3268.  
  3269.      You could extend the program in example 5-11 to fade many colors
  3270. simultaneously.  This is an attractive way of turning an image into a blank
  3271. screen.  Note that example 5-11 would also work in video modes 17 and 18 as
  3272. long as you just use the first 16 video DAC registers.
  3273.  
  3274.  
  3275. Defining All Palette Registers
  3276.  
  3277.      Fastgraph includes a routine fg_palettes that defines all 16 palette
  3278. registers in modes 9, 13, 14, 16, and 18.  You can also use the fg_palettes
  3279. routine to define the first 16 video DAC registers in modes 19, 20, and 21.
  3280. It has no effect in other video modes.
  3281.  
  3282.      Using fg_palettes is considerably faster than calling the fg_palette
  3283. routine 16 times.  The argument to the fg_palettes routine is a 16-element
  3284. integer array that contains the color values assigned respectively to palette
  3285. registers (or video DAC registers) 0 to 15.  Example 5-12 demonstrates how to
  3286. zero the palette registers (that is, change them all to black) in mode 13.
  3287.  
  3288.                                              Chapter 5:  The Use of Color   63
  3289.                                 Example 5-12.
  3290.  
  3291.               int zeroes[] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  3292.  
  3293.               main()
  3294.               {
  3295.                  int mode;
  3296.  
  3297.                  mode = fg_getmode();
  3298.                  fg_setmode(13);
  3299.  
  3300.                  fg_palettes(zeroes);
  3301.  
  3302.                  fg_setmode(mode);
  3303.                  fg_reset();
  3304.               }
  3305.  
  3306.  
  3307. Of course, as example 5-12 is written, it appears to do nothing more than
  3308. blank the screen.  Its purpose is just to show an example of the fg_palettes
  3309. routine.
  3310.  
  3311.  
  3312. Virtual Colors
  3313.  
  3314.      By this time it should be apparent the use of color is rather specific
  3315. to each graphics video mode.  One of the most obvious differences is the
  3316. number of available colors in each mode; it ranges from 2 to 256.  By
  3317. available colors, we mean the number of colors that can be displayed
  3318. simultaneously.
  3319.  
  3320.      To simplify programming in graphics modes, Fastgraph provides 256
  3321. virtual colors.  The virtual colors are used in the graphics video modes
  3322. having fewer than 256 available colors.  Virtual colors allow you to use 256
  3323. color indices in all graphics modes, even if a particular mode does not have
  3324. 256 available colors.
  3325.  
  3326.      When you establish a video mode with the fg_setmode routine, Fastgraph
  3327. initializes all of the virtual color indices.  It does this by replicating
  3328. the video mode's color values through the 256 virtual color indices.  For
  3329. example, the CGA color modes (4 and 5) have four color values, numbered 0
  3330. through 3.  In these modes, the fg_setmode routine initializes color indices
  3331. 0, 4, 8, ... , 252 to 0; color indices 1, 5, 9, ... , 253 to 1; color indices
  3332. 2, 6, 10, ... , 254 to 2; and color indices 3, 7, 11, ... , 255 to 3.
  3333. Similarly, in 16-color graphics modes the color indices 0, 16, 32, ... , 240
  3334. are set to 0, and so forth.  An analysis of the color value sequences reveals
  3335. an often useful feature:  by default, virtual color 0 is black and virtual
  3336. color 15 is white or bright white in all graphics video modes.
  3337.  
  3338.      It is thus possible to write a multiple-mode program using the same
  3339. color indices for each graphics mode.  For example, a program that contains
  3340. the statement fg_setcolor(5) would produce subsequent graphics in color 5
  3341. (magenta by default) when running in a 16-color graphics mode.  It would
  3342. produce subsequent graphics in color 1 (light cyan by default) when running
  3343. in a CGA color mode.  This is because 1 is the default value assigned to
  3344. virtual color index 5 in the CGA color modes.
  3345. 64   Fastgraph User's Guide
  3346.  
  3347.      The fg_setmode routine establishes default values for the 256 virtual
  3348. color indices, but it might be desirable to assign other available colors to
  3349. them.  Going back to the discussion in the previous paragraph, color number 2
  3350. is light magenta in the default CGA mode 4 palette.  It might make more sense
  3351. if the color value 2 were assigned to virtual color index 5, as this would
  3352. make the graphics drawn in color 5 the same color in mode 4 as in other color
  3353. modes.  The Fastgraph routine fg_defcolor is provided for this purpose.
  3354.  
  3355.      The fg_defcolor routine assigns a color value to a virtual color index.
  3356. It has two arguments:  the first specifies the virtual color index (between 0
  3357. and 255), and the second specifies the color value (between 0 and the number
  3358. of available colors in the current video mode).  For example, the statement
  3359.  
  3360.                               fg_defcolor(5,2);
  3361.  
  3362. would assign the color value 2 to the color index 5.  Another Fastgraph
  3363. routine, fg_getindex, returns the current value assigned to a specified color
  3364. index.  After executing the above call to fg_defcolor, the statement
  3365.  
  3366.                            color = fg_getindex(5);
  3367.  
  3368. would store the value 2 (the current value of color index 5) in the integer
  3369. variable color.
  3370.  
  3371.      It is important to understand the difference between virtual colors and
  3372. palette registers.  Modifying the value of a palette register changes the
  3373. color of all pixels already drawn using that palette.  Modifying a virtual
  3374. color index does not do this; it only specifies any graphics drawn in that
  3375. color from this point on will appear in the new color.
  3376.  
  3377.      Example 5-13 demonstrates the use of virtual colors in mode 4.  After
  3378. establishing the video mode, the program uses the fg_defcolor routine to
  3379. define virtual color indices 0 and 255 to be 1, which by default is light
  3380. cyan in mode 4.  It then draws characters using color indices 0, 1, and 255,
  3381. and in each case the characters appear in light cyan.  Finally, the program
  3382. restores the original video mode and screen attributes before returning to
  3383. DOS.
  3384.  
  3385.                                 Example 5-13.
  3386.  
  3387.                            main()
  3388.                            {
  3389.                               int mode;
  3390.  
  3391.                               mode = fg_getmode();
  3392.                               fg_setmode(4);
  3393.                               fg_defcolor(0,1);
  3394.                               fg_defcolor(255,1);
  3395.  
  3396.                               fg_setcolor(0);
  3397.                               fg_text("0",1);
  3398.                               fg_setcolor(1);
  3399.                               fg_text(" 1",2);
  3400.                               fg_setcolor(255);
  3401.                               fg_text(" 255",4);
  3402.                               fg_waitkey();
  3403.  
  3404.                                              Chapter 5:  The Use of Color   65
  3405.  
  3406.  
  3407.                               fg_setmode(mode);
  3408.                               fg_reset();
  3409.                            }
  3410.  
  3411.  
  3412. A Multiple-Mode Example
  3413.  
  3414.      Even though the color capabilities differ considerably between the
  3415. supported video modes, Fastgraph makes it easy to write a program that runs
  3416. in many video modes.  This section will present an example of such a program.
  3417.  
  3418.      Example 5-14 illustrates a program that will run in any of Fastgraph's
  3419. supported video modes.  The program first asks for the video mode number,
  3420. checks if the mode number is valid, and then checks if the requested mode is
  3421. available on the user's system.  After doing this, the program establishes
  3422. the video mode and performs its mode-specific code.  It then displays a brief
  3423. message that includes the video mode number in which the program is running.
  3424. This information remains on the screen until a key is pressed, at which time
  3425. the program restores the original video mode and screen attributes before
  3426. returning to DOS.
  3427.  
  3428.                                 Example 5-14.
  3429.  
  3430.        main()
  3431.        {
  3432.           int mode, old_mode;
  3433.           char string[5];
  3434.  
  3435.        /* Ask for the video mode number */
  3436.           printf("Which video mode? ");
  3437.           scanf("%d",&mode);
  3438.  
  3439.        /* Make sure the entered value is valid */
  3440.           if (mode < 0 || mode > 21) {
  3441.              printf("%d is not a valid video mode number.\n",mode);
  3442.              exit();
  3443.              }
  3444.  
  3445.        /* Make sure the requested video mode is available */
  3446.           if (fg_testmode(mode,1) == 0) {
  3447.              printf("Mode %d is not available on this system.\n",mode);
  3448.              exit();
  3449.              }
  3450.  
  3451.        /* Establish the video mode */
  3452.           old_mode = fg_getmode();
  3453.           fg_setmode(mode);
  3454.  
  3455.        /* Perform mode-specific initializations */
  3456.           if (mode <= 3 || mode == 7)   /* text modes */
  3457.              fg_cursor(0);
  3458.  
  3459.           else if (mode == 4 || mode == 5) { /* CGA color modes */
  3460.              fg_palette(0,0);
  3461.              fg_defcolor(14,3);
  3462.  
  3463. 66   Fastgraph User's Guide
  3464.  
  3465.              }
  3466.  
  3467.           else if (mode == 6) {         /* CGA two-color mode */
  3468.              fg_palette(0,14);
  3469.              fg_defcolor(14,1);
  3470.              }
  3471.  
  3472.           else if (mode == 11)          /* Hercules mode */
  3473.              fg_defcolor(14,1);
  3474.  
  3475.           else if (mode == 12)          /* Hercules low-res mode */
  3476.              fg_defcolor(14,3);
  3477.  
  3478.           else if (mode == 17) {        /* VGA two-color mode */
  3479.              fg_palette(1,14);
  3480.              fg_setrgb(14,63,63,21);
  3481.              fg_defcolor(14,1);
  3482.              }
  3483.  
  3484.        /* Display a message that includes the video mode number */
  3485.           fg_setcolor(14);
  3486.           fg_text("I'm running in mode ",20);
  3487.           sprintf(string,"%d. ",mode);
  3488.           fg_text(string,3);
  3489.  
  3490.        /* Wait for a keystroke */
  3491.           fg_waitkey();
  3492.  
  3493.        /* Restore the original video mode and screen attributes */
  3494.           fg_setmode(old_mode);
  3495.           fg_reset();
  3496.        }
  3497.  
  3498.  
  3499.      Example 5-14 displays its message in yellow for those video modes that
  3500. offer color.  In monochrome video modes, it displays the message in normal
  3501. intensity.  The program uses virtual color 14, which by default is yellow in
  3502. many video modes; the mode-specific code in example 5-14 makes color 14
  3503. yellow in other video modes.  In text video modes (modes 0 to 3 and 7), the
  3504. program uses the fg_cursor routine to make the cursor invisible.  In CGA
  3505. color modes (modes 4 and 5), the program uses the fg_palette routine to
  3506. select a CGA palette that contains yellow as color 3 and then uses
  3507. fg_defcolor to assign color 3 to virtual color 14.  In CGA two-color mode
  3508. (mode 6), the program uses the fg_palette routine to make color 1 yellow and
  3509. then uses fg_defcolor to assign color 1 to virtual color 14.  In the Hercules
  3510. modes (modes 11 and 12), the program uses the fg_defcolor routine to assign
  3511. the value for normal intensity pixels to color 14.  In VGA two-color mode
  3512. (mode 17), the program uses the fg_palette routine to assign video DAC
  3513. register 14 to palette register 1.  It then defines video DAC register 14 to
  3514. be yellow with the fg_setrgb routine and finally uses fg_defcolor to assign
  3515. color 1 (that is, palette register 1) to virtual color 14.  In all of the
  3516. other video modes, color 14 is yellow by default.
  3517.                                              Chapter 5:  The Use of Color   67
  3518.  
  3519.  
  3520. Summary of Color-Related Routines
  3521.  
  3522.      This section summarizes the functional descriptions of the Fastgraph
  3523. routines presented in this chapter.  More detailed information about these
  3524. routines, including their arguments and return values, may be found in the
  3525. Fastgraph Reference Manual.
  3526.  
  3527.      FG_DEFCOLOR assigns a color value to a virtual color index.  This
  3528. routine is only meaningful in the graphics video modes that have fewer than
  3529. 256 available colors.
  3530.  
  3531.      FG_GETCOLOR returns the current text attribute (in text modes) or color
  3532. index (in graphics modes), as specified in the most recent call to fg_setattr
  3533. or fg_setcolor.
  3534.  
  3535.      FG_GETINDEX returns the color value assigned to a specified virtual
  3536. color index.  In text modes and in graphics modes that have 256 available
  3537. colors, this routine returns the value passed to it.
  3538.  
  3539.      FG_GETRGB returns the red, green, and blue color components for a
  3540. specified video DAC register.  This routine is only meaningful in VGA and
  3541. MCGA graphics modes.
  3542.  
  3543.      FG_PALETTE has different functions depending on the current graphics
  3544. video mode.  For the CGA four-color modes, it establishes the current palette
  3545. (of six available) and defines the background color for that palette.  In the
  3546. CGA two-color mode, it defines the foreground color.  For the Tandy/PCjr,
  3547. EGA, and VGA graphics modes, it defines the value of a single palette
  3548. register.  For the 256-color MCGA and VGA graphics modes, it defines the
  3549. value of a single video DAC register.  The fg_palette routine has no effect
  3550. in text modes or Hercules graphics modes.
  3551.  
  3552.      FG_PALETTES defines all 16 palette registers (in Tandy/PCjr, EGA, and
  3553. VGA graphics modes), or the first 16 video DAC registers (in 256-color MCGA
  3554. and VGA graphics modes).  The fg_palettes routine has no effect in text
  3555. modes, CGA graphics modes, or Hercules graphics modes.
  3556.  
  3557.      FG_SETATTR establishes the current text attribute in text video modes.
  3558. This routine has no effect in graphics modes.
  3559.  
  3560.      FG_SETCOLOR establishes the current color index (which may be a virtual
  3561. color index in graphics modes).  In text modes, the fg_setcolor routine
  3562. provides an alternate method of establishing the current text attribute.
  3563.  
  3564.      FG_SETRGB defines the value of a single palette register (in Tandy/PCjr
  3565. and EGA graphics modes) or video DAC register (in VGA and MCGA modes) by
  3566. specifying its red, green, and blue color components.  The fg_setrgb routine
  3567. has no effect in text modes, CGA graphics modes, or Hercules graphics modes.
  3568. 68   Fastgraph User's Guide
  3569.  
  3570.  
  3571. Chapter 6
  3572.  
  3573. Graphics Fundamentals
  3574. 70   Fastgraph User's Guide
  3575.  
  3576.  
  3577. Overview
  3578.  
  3579.      This chapter describes Fastgraph's fundamental graphics routines,
  3580. sometimes called graphics primitives.  These routines perform such functions
  3581. as clearing the screen, drawing points, drawing solid and dashed lines,
  3582. drawing closed shapes (polygons, circles, and ellipses), drawing rectangles
  3583. (solid and dithered), and filling arbitrary regions.  Most of these routines
  3584. have no effect in text video modes, but there are a few exceptions, and they
  3585. will be noted in the descriptions of those routines.
  3586.  
  3587.  
  3588. Clearing the Screen
  3589.  
  3590.      The Fastgraph routine fg_erase clears the entire screen in any video
  3591. mode.  In text modes, fg_erase clears the screen by storing a space character
  3592. (ASCII 32) with a gray foreground attribute in each character cell.  In
  3593. graphics modes, fg_erase clears the screen by setting each pixel to zero.
  3594. This of course causes each pixel to be displayed its background color.  The
  3595. fg_erase routine has no arguments.
  3596.  
  3597.  
  3598. Clipping
  3599.  
  3600.      The suppression of graphics outside a pre-defined area is called
  3601. clipping.  Many of Fastgraph's graphics-oriented routines provide clipping,
  3602. either automatically or through a special version of the routine.
  3603.  
  3604.      Fastgraph includes two routines, fg_setclip and fg_setclipw, to define a
  3605. rectangular clipping region.  The fg_setclip routine defines the clipping
  3606. region in screen space, while the fg_setclipw routine performs the same
  3607. function in world space.  Each routine takes four arguments:  the minimum x,
  3608. the maximum x, the minimum y, and the maximum y coordinate of the clipping
  3609. region.  The arguments are integer quantities for fg_setclip and floating
  3610. point quantities for fg_setclipw.  For example, the statement
  3611.  
  3612.                            fg_setclip(0,159,0,99);
  3613.  
  3614. would define the upper left quadrant of the screen as the clipping region in
  3615. a 320 by 200 graphics mode.
  3616.  
  3617.      An implicit clipping region equal to the entire screen is defined as
  3618. part of the fg_setmode routine's initializations.  Clipping is not supported
  3619. for text modes.
  3620.  
  3621.  
  3622. Points
  3623.  
  3624.      The Fastgraph routine fg_point provides the most fundamental graphics
  3625. operation -- setting a pixel to a specified color.  The fg_point routine has
  3626. two integer arguments.  The first specifies the pixel's x coordinate, and the
  3627. second its y coordinate.  The pixel is drawn using the current color value,
  3628. as specified in the most recent call to fg_setcolor.  There is also a world
  3629. space version of this routine, fg_pointw, that uses floating point arguments.
  3630.  
  3631.      Another Fastgraph routine is available for reading a pixel's color
  3632. value.  The fg_getpixel routine has two integer arguments that specify the
  3633.                                         Chapter 6:  Graphics Fundamentals   71
  3634.  
  3635. (x,y) coordinates for the pixel of interest.  There is no world space version
  3636. of the fg_getpixel routine, but you can obtain a pixel's color value in world
  3637. space by applying the fg_xscreen and fg_yscreen functions to the world space
  3638. coordinates and passing the resulting values to fg_getpixel.
  3639.  
  3640.      Example 6-1 uses the fg_point routine to draw 100 random points in
  3641. random colors.  It also uses the fg_getpixel routine to insure no two points
  3642. are adjacent.  The program establishes a graphics video mode with the
  3643. fg_automode and fg_setmode routines.  Next, it determines the maximum color
  3644. value for the selected video mode; note if we used virtual colors (color
  3645. indices above the maximum color value), some of the colors would be the
  3646. background color and would thus produce invisible points.  The main part of
  3647. the program is a while loop that first generates a random pair of (x,y)
  3648. screen coordinates.  It then calls the fg_getpixel routine to check the
  3649. pixels at (x,y) and the eight adjacent positions.  If none of these pixels
  3650. are set, the program generates a random color value and draws a point in that
  3651. color.  After doing this 100 times, the program waits for a keystroke,
  3652. restores the original video mode and screen attributes, and then returns to
  3653. DOS.
  3654.  
  3655.                               Example 6-1.
  3656.  
  3657. main()
  3658. {
  3659.    int area;
  3660.    int color, old_color;
  3661.    int left;
  3662.    int max_color, max_x, max_y;
  3663.    int new_mode, old_mode;
  3664.    int x, y;
  3665.  
  3666.    old_mode = fg_getmode();
  3667.    new_mode = fg_automode();
  3668.    fg_setmode(new_mode);
  3669.  
  3670.    if (new_mode == 4)
  3671.       max_color = 3;
  3672.    else if (new_mode == 11 || new_mode == 17)
  3673.       max_color = 1;
  3674.  
  3675. 72   Fastgraph User's Guide
  3676.  
  3677.    else if (new_mode == 19)
  3678.       max_color = 255;
  3679.    else
  3680.       max_color = 15;
  3681.  
  3682.    left = 100;
  3683.    max_x = fg_getmaxx() - 1;
  3684.    max_y = fg_getmaxy() - 1;
  3685.  
  3686.    while (left > 0) {
  3687.  
  3688.       x = random(1,max_x);
  3689.       y = random(1,max_y);
  3690.  
  3691.       area = fg_getpixel(x-1,y-1) + fg_getpixel(x,y-1) + fg_getpixel(x+1,y-1)
  3692.            + fg_getpixel(x-1,y)   + fg_getpixel(x,y)   + fg_getpixel(x+1,y)
  3693.            + fg_getpixel(x-1,y+1) + fg_getpixel(x,y+1) + fg_getpixel(x+1,y+1);
  3694.  
  3695.       if (area == 0) {
  3696.          color = random(1,max_color);
  3697.          fg_setcolor(color);
  3698.          fg_point(x,y);
  3699.          left--;
  3700.          }
  3701.  
  3702.       }
  3703.  
  3704.    fg_waitkey();
  3705.  
  3706.    fg_setmode(old_mode);
  3707.    fg_reset();
  3708. }
  3709.  
  3710. random(min,max)
  3711. int min, max;
  3712. {
  3713.    return((rand() % (max - min + 1)) + min);
  3714. }
  3715.  
  3716.                                         Chapter 6:  Graphics Fundamentals   73
  3717.  
  3718. The Graphics Cursor
  3719.  
  3720.      Many of Fastgraph's graphics routines depend on the position of the
  3721. graphics cursor as a reference point.  For example, Fastgraph includes
  3722. routines to draw lines from the graphics cursor position to a specified
  3723. position, and the image display routines discussed in chapter 9 display or
  3724. retrieve an image relative to the graphics cursor position.  The graphics
  3725. cursor is not a cursor in the true sense; it is simply a pair of (x,y)
  3726. coordinates with a special meaning.  The fg_setmode routine sets the graphics
  3727. cursor position to the screen space coordinates (0,0), and the fg_initw
  3728. routine sets it to the world space coordinates (0.0,0.0).
  3729.  
  3730.      Fastgraph includes four routines for changing the graphics cursor
  3731. position.  The fg_move routine sets it to an absolute screen space position,
  3732. while the fg_movew routine sets it to an absolute world space position.  The
  3733. fg_moverel routine sets the graphics cursor position to a screen space
  3734. position relative to its current position.  The fg_moverw routine does the
  3735. same in world space.
  3736.  
  3737.      Each of these routines has two arguments that specify the (x,y)
  3738. coordinates of the new position.  For the screen space routines, the
  3739. arguments are integer quantities.  For the world space routines, the
  3740. arguments are floating point quantities.
  3741.  
  3742.      You can obtain the screen space coordinates of the graphics cursor
  3743. position with the fg_getxpos and fg_getypos routines.  These routines have no
  3744. arguments and respectively return the x and y coordinates of the graphics
  3745. cursor position as the function value.  To obtain the world space coordinates
  3746. of the graphics cursor position, you can apply the fg_xworld and fg_yworld
  3747. functions to the return values of fg_getxpos and fg_getypos.
  3748. 74   Fastgraph User's Guide
  3749.  
  3750.  
  3751. Solid Lines
  3752.  
  3753.      Fastgraph includes four routines for drawing solid lines.  All four
  3754. routines draw lines in the current color value (as determined by the most
  3755. recent call to fg_setcolor) and observe the clipping limits.  The fg_draw
  3756. routine draws a line from the current graphics cursor position to an absolute
  3757. screen space position, while the fg_draww routine draws a line to an absolute
  3758. world space position.  The fg_drawrel routine draws a line from the current
  3759. graphics cursor position to a screen space position relative to it.  The
  3760. fg_drawrw routine does the same in world space.
  3761.  
  3762.      Each of these routines has two arguments that specify the (x,y)
  3763. coordinates of the destination position.  For the screen space routines, the
  3764. arguments are integer quantities.  For the world space routines, the
  3765. arguments are floating point quantities.  In either case the destination
  3766. position becomes the new graphics cursor position.  This makes it possible to
  3767. draw connected lines without calling a graphics cursor movement routine
  3768. between successive calls to a line drawing routine.
  3769.  
  3770.      Examples 6-2 and 6-3 each draw a pair of crossed lines that divide the
  3771. screen into quadrants.  Example 6-2 does this using the fg_move and fg_draw
  3772. routines, while example 6-3 uses the fg_moverel and fg_drawrel routines.
  3773. Both examples draw the lines in bright white, the default for color 15 in all
  3774. graphics video modes.
  3775.  
  3776.               Example 6-2.                          Example 6-3.
  3777.  
  3778.      main()                                main()
  3779.      {                                     {
  3780.         int max_x, max_y;                     int max_x, max_y;
  3781.         int mid_x, mid_y;                     int mid_x, mid_y;
  3782.         int new_mode, old_mode;               int new_mode, old_mode;
  3783.  
  3784.         old_mode = fg_getmode();              old_mode = fg_getmode();
  3785.         new_mode = fg_automode();             new_mode = fg_automode();
  3786.         fg_setmode(new_mode);                 fg_setmode(new_mode);
  3787.  
  3788.         max_x = fg_getmaxx();                 max_x = fg_getmaxx();
  3789.         max_y = fg_getmaxy();                 max_y = fg_getmaxy();
  3790.         mid_x = max_x / 2;                    mid_x = max_x / 2;
  3791.         mid_y = max_y / 2;                    mid_y = max_y / 2;
  3792.  
  3793.         fg_setcolor(15);                      fg_setcolor(15);
  3794.         fg_move(mid_x,0);                     fg_move(mid_x,0);
  3795.         fg_draw(mid_x,max_y);                 fg_drawrel(0,max_y);
  3796.         fg_move(0,mid_y);                     fg_moverel(-mid_x,-mid_y);
  3797.         fg_draw(max_x,mid_y);                 fg_drawrel(max_x,0);
  3798.  
  3799.         fg_waitkey();                         fg_waitkey();
  3800.  
  3801.         fg_setmode(old_mode);                 fg_setmode(old_mode);
  3802.         fg_reset();                           fg_reset();
  3803.      }                                     }
  3804.  
  3805.                                         Chapter 6:  Graphics Fundamentals   75
  3806.  
  3807.      Examples 6-4 and 6-5 are variations of example 6-2.  Example 6-4 uses
  3808. world space rather than screen space to draw the crossed lines.  Example 6-5
  3809. is identical to example 6-2 except it defines a clipping area to restrict
  3810. drawing to the upper left quadrant of the screen.  The clipping suppresses
  3811. the right half of the horizontal line and the lower half of the vertical
  3812. line.
  3813.  
  3814.              Example 6-4.                            Example 6-5.
  3815.  
  3816. main()                                      main()
  3817. {                                           {
  3818.    int new_mode, old_mode;                    int max_x, max_y;
  3819.                                               int mid_x, mid_y;
  3820.    old_mode = fg_getmode();                   int new_mode, old_mode;
  3821.    new_mode = fg_automode();
  3822.    fg_setmode(new_mode);                      old_mode = fg_getmode();
  3823.    fg_initw();                                new_mode = fg_automode();
  3824.    fg_setworld(-10.0,10.0,-10.0,10.0);        fg_setmode(new_mode);
  3825.  
  3826.    fg_setcolor(15);                           max_x = fg_getmaxx();
  3827.    fg_movew(0.0,10.0);                        max_y = fg_getmaxy();
  3828.    fg_draww(0.0,-10.0);                       mid_x = max_x / 2;
  3829.    fg_movew(-10.0,0.0);                       mid_y = max_y / 2;
  3830.    fg_draww(10.0,0.0);
  3831.                                               fg_setclip(0,mid_x,0,mid_y);
  3832.    fg_waitkey();
  3833.                                               fg_setcolor(15);
  3834.    fg_setmode(old_mode);                      fg_move(mid_x,0);
  3835.    fg_reset();                                fg_draw(mid_x,max_y);
  3836. }                                             fg_move(0,mid_y);
  3837.                                               fg_draw(max_x,mid_y);
  3838.  
  3839.                                               fg_waitkey();
  3840.  
  3841.                                               fg_setmode(old_mode);
  3842.                                               fg_reset();
  3843.                                            }
  3844.  
  3845. 76   Fastgraph User's Guide
  3846.  
  3847. Dashed Lines
  3848.  
  3849.      Fastgraph includes four routines for drawing dashed lines.  All four
  3850. routines draw lines in the current color value (as determined by the most
  3851. recent call to fg_setcolor) and observe the clipping limits.  The fg_dash
  3852. routine draws a dashed line from the current graphics cursor position to an
  3853. absolute screen space position, while the fg_dashw routine draws a dashed
  3854. line to an absolute world space position.  The fg_dashrel routine draws a
  3855. dashed line from the current graphics cursor position to a screen space
  3856. position relative to it.  The fg_dashrw routine does the same in world space.
  3857.  
  3858.      Each of these routines has three arguments.  The first two specify the
  3859. (x,y) coordinates of the destination position.  For the screen space
  3860. routines, these arguments are integer quantities.  For the world space
  3861. routines, these arguments are floating point quantities.  The third argument
  3862. is a 16-bit pattern that defines the appearance of the dashed line.  Bits
  3863. that are set in the pattern produce the visible part of the line, while bits
  3864. that are reset produce the invisible part.  This pattern is repeated as
  3865. necessary to draw the entire line.  For example, the pattern value 3333 hex
  3866. would produce a dashed line with the first two pixels off, the next two on,
  3867. the next two off, and so forth.  Similarly, the pattern value FFFF hex would
  3868. produce a solid line.
  3869.  
  3870.      The destination position passed to any of the dashed line routines
  3871. becomes the new graphics cursor position.  This makes it possible to draw
  3872. connected dashed lines without calling a graphics cursor movement routine
  3873. between successive calls to a line drawing routine.
  3874.  
  3875.      Example 6-6 draws a pair of crossed dashed lines that divide the screen
  3876. into quadrants.  It does this using the fg_move and fg_dash routines and
  3877. draws the lines in bright white, the default for color 15 in all graphics
  3878. video modes.  The dash pattern for each line is 3333 hex, which alternates
  3879. two pixels off and on.
  3880.  
  3881.                                  Example 6-6.
  3882.  
  3883.                        main()
  3884.                        {
  3885.                           int max_x, max_y;
  3886.                           int mid_x, mid_y;
  3887.                           int new_mode, old_mode;
  3888.  
  3889.                           old_mode = fg_getmode();
  3890.  
  3891.                                         Chapter 6:  Graphics Fundamentals   77
  3892.  
  3893.                           new_mode = fg_automode();
  3894.                           fg_setmode(new_mode);
  3895.  
  3896.                           max_x = fg_getmaxx();
  3897.                           max_y = fg_getmaxy();
  3898.                           mid_x = max_x / 2;
  3899.                           mid_y = max_y / 2;
  3900.  
  3901.                           fg_setcolor(15);
  3902.                           fg_move(mid_x,0);
  3903.                           fg_dash(mid_x,max_y,0x3333);
  3904.                           fg_move(0,mid_y);
  3905.                           fg_dash(max_x,mid_y,0x3333);
  3906.  
  3907.                           fg_waitkey();
  3908.  
  3909.                           fg_setmode(old_mode);
  3910.                           fg_reset();
  3911.                        }
  3912.  
  3913.  
  3914.  
  3915. Polygons, Circles, and Ellipses
  3916.  
  3917.      Fastgraph includes routines for drawing three types of closed shapes:
  3918. polygons, circles, and ellipses.  Both screen space and world space versions
  3919. of these routines are available.  All of the routines for drawing closed
  3920. shapes observe the clipping limits.
  3921.  
  3922.      The fg_polygon routine draws an unfilled polygon in screen space.
  3923. Fg_polygon requires an array of integer x coordinates as its first argument,
  3924. and an array of integer y coordinates as its second argument.  Each (x,y)
  3925. coordinate pair from the two arrays is treated as a polygon vertex.  In other
  3926. words, the x coordinate of the first polygon vertex is the first element of
  3927. the x coordinate array, and the y coordinate of the first vertex is the first
  3928. element of the y coordinate array.  Similarly, the second elements of each
  3929. array define the second vertex, and so forth.  The third argument for
  3930. fg_polygon is an integer quantity that specifies the number of elements in
  3931. the two coordinate arrays.
  3932.  
  3933.      Another routine, fg_polygonw, draws an unfilled polygon in world space.
  3934. The fg_polygonw routine is identical to the fg_polygon routine, except its x
  3935. and y coordinate arrays must contain floating point values instead of
  3936. integers.
  3937.  
  3938.      The drawing of the polygon begins at the graphics cursor position.  The
  3939. routine then draws a solid line in the current color to the first vertex,
  3940. then to the second vertex, and continues in this manner up to the last
  3941. vertex.  If necessary, fg_polygon and fg_polygonw draw a line connecting the
  3942. last vertex and the original graphics cursor position.  This last operation
  3943. closes the polygon and effectively leaves the graphics cursor position
  3944. unchanged.
  3945.  
  3946.      Example 6-7 illustrates the use of the fg_polygon routine in the EGA
  3947. monochrome or enhanced modes (mode 15 and 16).  The program exits if neither
  3948. of these video modes are available.
  3949. 78   Fastgraph User's Guide
  3950.  
  3951.                                  Example 6-7.
  3952.  
  3953.              #define VERTICES 10
  3954.  
  3955.              int x[] = {200,300,400,400,300,240,160,160,200,200};
  3956.              int y[] = {100, 80,100,220,320,320,240,200,160,160};
  3957.  
  3958.              main()
  3959.              {
  3960.                 int old_mode;
  3961.  
  3962.                 old_mode = fg_getmode();
  3963.  
  3964.                 if (fg_testmode(16,1))
  3965.                    fg_setmode(16);
  3966.                 else if (fg_testmode(15,1))
  3967.                    fg_setmode(15);
  3968.                 else {
  3969.                    printf("This program requires a 640 x 350 ");
  3970.                    printf("EGA graphics mode.\n");
  3971.                    exit();
  3972.                    }
  3973.  
  3974.                 fg_setcolor(1);
  3975.                 fg_polygon(x,y,VERTICES);
  3976.                 fg_waitkey();
  3977.  
  3978.                 fg_setmode(old_mode);
  3979.                 fg_reset();
  3980.              }
  3981.  
  3982.  
  3983.      The fg_circle routine draws an unfilled circle in screen space, centered
  3984. at the graphics cursor position, using the current color.  The routine's only
  3985. argument specifies the circle's radius in horizontal screen space units.
  3986. Another routine, fg_circlew, draws an unfilled circle where the radius is
  3987. measured in horizontal world space units.  Both routines leave the graphics
  3988. cursor position unchanged.
  3989.  
  3990.      The fg_ellipse routine draws an unfilled ellipse in screen space,
  3991. centered at the graphics cursor position, using the current color.  The
  3992. routine requires two arguments that respectively specify the length of its
  3993. horizontal and vertical semi-axes.  In other words, the first argument is the
  3994. absolute distance from the center of the ellipse to its horizontal extremity,
  3995. and the second argument is the absolute distance from the center of the
  3996. ellipse to its vertical extremity.  Another routine, fg_ellipsew, draws an
  3997. unfilled ellipse in world space.  Both routines leave the graphics cursor
  3998. position unchanged.
  3999.  
  4000.      Example 6-8 illustrates the use of the fg_circlew and fg_ellipsew
  4001. routines.  The program first uses the fg_automode routine to propose a
  4002. graphics video mode and then uses the fg_setmode routine to select that video
  4003. mode.  It then makes color 15 the current color, which by default is bright
  4004. white in all color graphics modes and "on" in the monochrome graphics modes.
  4005. Next, it establishes a 200-unit by 200-unit world space coordinate system.
  4006. The program then uses fg_ellipsew to draw an ellipse and fg_circlew to draw a
  4007. circle, both centered at the middle of the screen (which is the origin of our
  4008.                                         Chapter 6:  Graphics Fundamentals   79
  4009.  
  4010. world space coordinate system).  The circle has a radius of 1/16 the width of
  4011. the screen (12.5 horizontal world space units), and the ellipse is
  4012. horizontally inscribed within the circle.
  4013.  
  4014.      Example 6-9 illustrates the use of the fg_circle and fg_ellipse
  4015. routines.  It is functionally identical to the program of example 6-8, but it
  4016. uses screen space rather than world space coordinates to draw the circle and
  4017. ellipse.  Note the arguments to the fg_circle and fg_ellipse routines are
  4018. dependent on the maximum x and y coordinates of the selected video mode.  If
  4019. we didn't compute these arguments in this manner, the actual size of the
  4020. circle and ellipse would be proportional to the pixel resolution of the video
  4021. mode.  No such dependency exists when using world space, but we pay a price
  4022. for this feature in slightly slower execution speed.
  4023.  
  4024.                Example 6-8.                            Example 6-9.
  4025.  
  4026. main()                                       main()
  4027. {                                            {
  4028.    int old_mode;                                int mid_x, mid_y;
  4029.                                                 int old_mode;
  4030.    old_mode = fg_getmode();                     int x,y;
  4031.    fg_setmode(fg_automode());
  4032.    fg_setcolor(15);                             old_mode = fg_getmode();
  4033.                                                 fg_setmode(fg_automode());
  4034.    fg_initw();                                  fg_setcolor(15);
  4035.    fg_setworld(-100.0,100.0,-100.0,100.0);
  4036.                                                 mid_x = fg_getmaxx() / 2;
  4037.    fg_movew(0.0,0.0);                           mid_y = fg_getmaxy() / 2;
  4038.    fg_ellipsew(12.5,12.5);                      x = mid_x / 8;
  4039.    fg_circlew(12.5);                            y = mid_y / 8;
  4040.  
  4041.    fg_waitkey();                                fg_move(mid_x,mid_y);
  4042.                                                 fg_ellipse(x,y);
  4043.    fg_setmode(old_mode);                        fg_circle(x);
  4044.    fg_reset();
  4045. }                                               fg_waitkey();
  4046.                                                 fg_setmode(old_mode);
  4047.                                                 fg_reset();
  4048.                                              }
  4049.  
  4050. 80   Fastgraph User's Guide
  4051.  
  4052. Solid Rectangles
  4053.  
  4054.      Fastgraph includes four routines for drawing solid rectangles, two for
  4055. screen space and two for world space, with and without clipping.  None of
  4056. these routines affect the graphics cursor position.
  4057.  
  4058.      The fg_rect routine draws a solid rectangle in screen space, without
  4059. regard to the clipping limits, using the current color.  Fg_rect requires
  4060. four integer arguments that respectively define the minimum x, maximum x,
  4061. minimum y, and maximum y screen space coordinates of the rectangle.  The
  4062. minimum coordinates must be less than or equal to the maximum coordinates, or
  4063. else the results are unpredictable.  The fg_clprect routine is identical in
  4064. all respects to the fg_rect routine, except it observes the clipping limits.
  4065.  
  4066.      The world space versions of the solid rectangle drawing routines are
  4067. fg_rectw and fg_clprectw.  Like fg_rect and fg_clprect, they require four
  4068. arguments that define the extremes of the rectangle, but the arguments are
  4069. floating point world space coordinates.
  4070.  
  4071.      You can also use the fg_rect routine in text modes.  When used in a text
  4072. mode, fg_rect expects its four arguments to be expressed in character space
  4073. (that is, rows and columns) rather than screen space.  This means the four
  4074. arguments respectively specify the minimum column, maximum column, minimum
  4075. row, and maximum row of the rectangle.  Fastgraph constructs the rectangle by
  4076. storing the solid block character (ASCII decimal value 219) in each character
  4077. cell comprising the rectangle.  The rectangle is drawn using the current
  4078. character attribute, but because the solid block character occupies the
  4079. entire character cell, the background component of the attribute is
  4080. essentially meaningless.
  4081.  
  4082.      Example 6-10 demonstrates the use of the fg_rect routine by drawing 200
  4083. random-size rectangles in random colors.  The program first uses the
  4084. fg_automode routine to propose a graphics video mode and then uses the
  4085. fg_setmode routine to select that video mode.  Next, it determines the
  4086. horizontal and vertical screen resolution for the selected video mode, using
  4087. the fg_getmaxx and fg_getmaxy routines.  The main part of the program is a
  4088. for loop that generates a random rectangle in each iteration.  Inside the
  4089. loop, the C library function rand is used to randomly generate the extremes
  4090. of the rectangle.  If necessary, the program then exchanges the coordinates
  4091. to make the minimum coordinates less than or equal to the maximum
  4092. coordinates.  Finally, it again uses rand to generate a random color number
  4093. between 0 and 15, and then draws the rectangle in that color.  After drawing
  4094.                                         Chapter 6:  Graphics Fundamentals   81
  4095.  
  4096. all 200 rectangles, the program restores the original video mode and screen
  4097. attributes before returning to DOS.
  4098.  
  4099.                                 Example 6-10.
  4100.  
  4101.             #define RECTANGLES 200
  4102.             #define SWAP(a,b,temp) { temp = a; a = b; b = temp; }
  4103.  
  4104.             main()
  4105.             {
  4106.                int i;
  4107.                int minx, maxx, miny, maxy;
  4108.                int old_mode;
  4109.                int temp;
  4110.                int xres, yres;
  4111.  
  4112.                old_mode = fg_getmode();
  4113.                fg_setmode(fg_automode());
  4114.  
  4115.                xres = fg_getmaxx() + 1;
  4116.                yres = fg_getmaxy() + 1;
  4117.  
  4118.                for (i = 0; i < RECTANGLES; i++) {
  4119.                   minx = rand() % xres;
  4120.                   maxx = rand() % xres;
  4121.                   miny = rand() % yres;
  4122.                   maxy = rand() % yres;
  4123.                   if (minx > maxx)
  4124.                      SWAP(minx,maxx,temp);
  4125.                   if (miny > maxy)
  4126.                      SWAP(miny,maxy,temp);
  4127.                   fg_setcolor(rand()%16);
  4128.                   fg_rect(minx,maxx,miny,maxy);
  4129.                   }
  4130.  
  4131.                fg_setmode(old_mode);
  4132.                fg_reset();
  4133.             }
  4134.  
  4135.  
  4136.  
  4137. Dithered Rectangles
  4138.  
  4139.      The process of alternating different color pixels across a region of the
  4140. display area is called dithering.  This technique is especially useful in the
  4141. graphics modes with few colors, such as CGA and Hercules modes, because you
  4142. can simulate additional colors through effective uses of dithering.
  4143. Fastgraph includes two routines for drawing dithered rectangles, one for
  4144. screen space and one for world space.  Neither routine observes the clipping
  4145. limits, nor do they affect the graphics cursor position.
  4146.  
  4147.      The fg_drect routine draws a dithered rectangle in screen space.  Like
  4148. the fg_rect routine, fg_drect requires four integer arguments that
  4149. respectively define the minimum x, maximum x, minimum y, and maximum y screen
  4150. space coordinates of the rectangle.  The minimum coordinates must be less
  4151. than or equal to the maximum coordinates, or else the results are
  4152. unpredictable.  However, fg_drect also requires a fifth argument that defines
  4153. 82   Fastgraph User's Guide
  4154.  
  4155. the dithering matrix, which in turn determines the pixel pattern used to
  4156. build the dithered rectangle.  The size and format of the dithering matrix
  4157. are dependent on the video mode.
  4158.  
  4159.      The world space version of the dithered rectangle drawing routine is
  4160. fg_drectw.  Like fg_drect, it requires four arguments that define the
  4161. extremes of the rectangle, and a fifth argument that defines the dithering
  4162. matrix.
  4163.  
  4164.      As mentioned earlier, the size and format of the dithering matrix are
  4165. dependent on the video mode.  The dithering matrix is a four byte array in
  4166. all video modes except the 256 color graphics modes (modes 19, 20, and 21),
  4167. where it is an eight byte array.  This array contains a pixel pattern that
  4168. fg_drect or fg_drectw replicates across the rectangle's area.  The structure
  4169. of the dithering matrix closely mimics the structure of video memory in each
  4170. graphics mode.
  4171.  
  4172.      The remainder of this section will present some simple mode-specific
  4173. examples to illustrate the structure of the dithering matrix in the different
  4174. graphics modes.  Suppose we would like to produce a "checkerboard" of light
  4175. blue and bright white pixels.  That is, in a given row of a rectangle,
  4176. consecutive pixels will alternate between these two colors.  Additionally,
  4177. the pattern for adjacent rows will be shifted such that there will always be
  4178. a bright white pixel above and below a light blue pixel, and vice versa.
  4179. Hence this pixel pattern would look something like
  4180.  
  4181.                                    B W B W
  4182.                                    W B W B
  4183.                                    B W B W
  4184.                                    W B W B
  4185.  
  4186. where each B represents a light blue pixel, and each W represents a bright
  4187. white pixel.  The following examples describe the dithering matrix that could
  4188. be used to produce such a pixel pattern in each graphics mode.
  4189.  
  4190. CGA Four-Color Graphics Modes
  4191.  
  4192.      The CGA four-color graphics modes (modes 4 and 5) use a four-byte
  4193. dithering matrix that Fastgraph treats as a four-row by one-column array.
  4194. Since each pixel in modes 4 and 5 requires two bits of video memory, each
  4195. byte of the dithering matrix holds four pixels.  Thus the pixel
  4196. representation of the dithering matrix would appear as shown below on the
  4197. left; its translation to numeric values appears on the right.
  4198.  
  4199.  
  4200.                    [3]   B W B W       [3]   01 11 01 11
  4201.  
  4202.                    [2]   W B W B       [2]   11 01 11 01
  4203.  
  4204.                    [1]   B W B W       [1]   01 11 01 11
  4205.  
  4206.                    [0]   W B W B       [0]   11 01 11 01
  4207.  
  4208.  
  4209. Because modes 4 and 5 do not offer a light blue color, we've used light cyan
  4210. (color value 1 in palette 1) to approximate light blue.  The B pixels thus
  4211. translate to color value 1, or 01 binary.  Bright white is available as color
  4212.                                         Chapter 6:  Graphics Fundamentals   83
  4213.  
  4214. value 3 in palette 1, so the W pixels translate to color value 3, or 11
  4215. binary.  The hexadecimal equivalent of the binary value 11011101 (for array
  4216. elements [0] and [2]) is DD, and the hexadecimal equivalent of the binary
  4217. value 01110111 (for array elements [1] and [3]) is 77.  As shown in example
  4218. 6-11, these are precisely the values assigned to the elements of the
  4219. dithering matrix.
  4220.  
  4221.      Example 6-11 uses mode 4 to display a 50-pixel by 50-pixel dithered
  4222. rectangle in the upper left corner of the screen.  The dithering matrix
  4223. represents the blue and white checkerboard pattern discussed in the preceding
  4224. paragraph.
  4225.  
  4226.                                 Example 6-11.
  4227.  
  4228.                        main()
  4229.                        {
  4230.                           char matrix[4];
  4231.                           int old_mode;
  4232.  
  4233.                           old_mode = fg_getmode();
  4234.                           fg_setmode(4);
  4235.  
  4236.                           matrix[0] = matrix[2] = 0xDD;
  4237.                           matrix[1] = matrix[3] = 0x77;
  4238.                           fg_drect(0,49,0,49,matrix);
  4239.  
  4240.                           fg_waitkey();
  4241.  
  4242.                           fg_setmode(old_mode);
  4243.                           fg_reset();
  4244.                        }
  4245.  
  4246.  
  4247. CGA Two-Color Graphics Mode
  4248.  
  4249.      The CGA two-color graphics mode (mode 6) uses a four-byte dithering
  4250. matrix that Fastgraph treats as a four-row by one-column array, as in the
  4251. other four-color CGA modes.  However, each pixel in mode 6 only requires one
  4252. bit of video memory, so each byte of the dithering matrix holds eight pixels.
  4253. Thus the pixel representation of the dithering matrix would appear as shown
  4254. below on the left; its translation to numeric values appears on the right.
  4255.  
  4256.  
  4257.              [3]   B W B W B W B W       [3]   0 1 0 1 0 1 0 1
  4258.  
  4259.              [2]   W B W B W B W B       [2]   1 0 1 0 1 0 1 0
  4260.  
  4261.              [1]   B W B W B W B W       [1]   0 1 0 1 0 1 0 1
  4262.  
  4263.              [0]   W B W B W B W B       [0]   1 0 1 0 1 0 1 0
  4264.  
  4265.  
  4266. Mode 6 obviously does not offer a light blue color, so we've used black
  4267. (color value 0) in its place.  The B pixels thus translate to color value 0.
  4268. Bright white is available as color value 1, so the W pixels translate to
  4269. color value 1.  The hexadecimal equivalent of the binary value 10101010 (for
  4270. array elements [0] and [2]) is AA, and the hexadecimal equivalent of the
  4271. 84   Fastgraph User's Guide
  4272.  
  4273. binary value 01010101 (for array elements [1] and [3]) is 55.  Thus, to make
  4274. example 6-11 run in mode 6, we only need to change the fg_setmode argument
  4275. from 4 to 6 and change the dithering matrix values as shown below.
  4276.  
  4277.  
  4278.                         matrix[0] = matrix[2] = 0xAA;
  4279.                         matrix[1] = matrix[3] = 0x55;
  4280.  
  4281.  
  4282. Tandy/PCjr 16-Color Graphics Mode
  4283.  
  4284.      The Tandy/PCjr 16-color graphics mode (mode 9) also uses a four-byte
  4285. dithering matrix that Fastgraph treats as a four-row by one-column array.
  4286. Each pixel in mode 9 requires four bits of video memory, so each byte of the
  4287. dithering matrix only holds two pixels.  Thus the pixel representation of the
  4288. dithering matrix would appear as shown below on the left; its translation to
  4289. numeric values appears on the right.
  4290.  
  4291.  
  4292.                       [3]   B W       [3]   1001 1111
  4293.  
  4294.                       [2]   W B       [2]   1111 1001
  4295.  
  4296.                       [1]   B W       [1]   1001 1111
  4297.  
  4298.                       [0]   W B       [0]   1111 1001
  4299.  
  4300.  
  4301. The B pixels translate to color value 9 (light blue), or 1001 binary, and the
  4302. W pixels translate to color value 15 (bright white), or 1111 binary.  The
  4303. hexadecimal equivalent of the binary value 11111001 (for array elements [0]
  4304. and [2]) is F9, and the hexadecimal equivalent of the binary value 10011111
  4305. (for array elements [1] and [3]) is 9F.  Thus, to make example 6-11 run in
  4306. mode 9, we only need to change the fg_setmode argument from 4 to 9 and change
  4307. the dithering matrix values as shown below.
  4308.  
  4309.  
  4310.                         matrix[0] = matrix[2] = 0xF9;
  4311.                         matrix[1] = matrix[3] = 0x9F;
  4312.  
  4313.  
  4314. Hercules Graphics Mode
  4315.  
  4316.      The size and format of the dithering matrix in the Hercules graphics
  4317. mode (mode 11) is identical to that of the CGA two-color mode (mode 6).
  4318. Please refer to page 83 for a discussion of mode 6 dithering.
  4319.  
  4320. Hercules Low-Resolution Graphics Mode
  4321.  
  4322.      The size and format of the dithering matrix in the Hercules low-
  4323. resolution graphics mode (mode 12) is identical to that of the CGA four-color
  4324. modes (modes 4 and 5).  As far as our checkerboard example goes, we'll use
  4325. black (color value 0) in place of light blue, and bold (color value 3)
  4326. instead of bright white.  Thus the B pixels translate to 00 binary, while the
  4327. W pixels translate to 11 binary.  The hexadecimal equivalent of the binary
  4328. value 11001100 (for array elements [0] and [2]) is CC, and the hexadecimal
  4329. equivalent of the binary value 00110011 (for array elements [1] and [3]) is
  4330.                                         Chapter 6:  Graphics Fundamentals   85
  4331.  
  4332. 33.  Thus, to make example 6-11 run in mode 12, we only need to change the
  4333. fg_setmode argument from 4 to 12 and change the dithering matrix values as
  4334. shown below.
  4335.  
  4336.  
  4337.                         matrix[0] = matrix[2] = 0xCC;
  4338.                         matrix[1] = matrix[3] = 0x33;
  4339.  
  4340.  
  4341. EGA and VGA Graphics Modes
  4342.  
  4343.      The native EGA and VGA graphics modes (modes 13 through 18) use a four-
  4344. byte dithering matrix that Fastgraph treats as a four-row by one-column
  4345. array.  Unlike the other graphics modes, which allow you to store pixels of
  4346. several colors in the dithering matrix, the EGA and VGA modes treat the
  4347. dithering matrix as a bit map for a specific color.  Since each color in the
  4348. dither pattern must be stored in a separate bit map (that is, in a separate
  4349. dithering matrix), it is necessary to call fg_drect once for each color.
  4350. Furthermore, you must use the fg_setcolor routine before each call to
  4351. fg_drect to define the color used with the dithering matrix.
  4352.  
  4353.      In all of the EGA and VGA graphics modes, each byte of the dithering
  4354. matrix is a bit map that represents eight pixels.  Using our familiar
  4355. checkerboard example, the pixel representation of the dithering matrix would
  4356. appear as shown below.
  4357.  
  4358.  
  4359.                            [3]   B W B W B W B W
  4360.  
  4361.                            [2]   W B W B W B W B
  4362.  
  4363.                            [1]   B W B W B W B W
  4364.  
  4365.                            [0]   W B W B W B W B
  4366.  
  4367.  
  4368. Translating this pattern to numeric values is simple.  Just construct one
  4369. dithering matrix for each color in the pattern (there are two colors in this
  4370. example), where pixels of the current color translate to 1, and other pixels
  4371. translate to 0.  Following our example, the translation for the B pixels
  4372. appears below on the left, while the translation for the W pixels appears on
  4373. the right.
  4374.  
  4375.  
  4376.              [3]   1 0 1 0 1 0 1 0       [3]   0 1 0 1 0 1 0 1
  4377.  
  4378.              [2]   0 1 0 1 0 1 0 1       [2]   1 0 1 0 1 0 1 0
  4379.  
  4380.              [1]   1 0 1 0 1 0 1 0       [1]   0 1 0 1 0 1 0 1
  4381.  
  4382.              [0]   0 1 0 1 0 1 0 1       [0]   1 0 1 0 1 0 1 0
  4383.  
  4384.  
  4385. The hexadecimal equivalent of the binary value 01010101 is 55, and the
  4386. hexadecimal equivalent of the binary value 10101010 is AA.  As shown in
  4387. example 6-12, these are precisely the values assigned to the elements of the
  4388. dithering matrices.
  4389. 86   Fastgraph User's Guide
  4390.  
  4391.  
  4392.      Example 6-12 uses mode 13 to display our light blue and bright white
  4393. checkerboard pattern.  Note it is necessary to call fg_drect twice -- once
  4394. for the light blue pixels (color value 9), and again for the bright white
  4395. pixels (color value 15).  Note also how fg_setcolor is used before each call
  4396. to fg_drect to define the color of the pixels fg_drect will display.
  4397.  
  4398.                                 Example 6-12.
  4399.  
  4400.                        main()
  4401.                        {
  4402.                           char matrix[4];
  4403.                           int old_mode;
  4404.  
  4405.                           old_mode = fg_getmode();
  4406.                           fg_setmode(13);
  4407.  
  4408.                           matrix[0] = matrix[2] = 0x55;
  4409.                           matrix[1] = matrix[3] = 0xAA;
  4410.                           fg_setcolor(9);
  4411.                           fg_drect(0,49,0,49,matrix);
  4412.  
  4413.                           matrix[0] = matrix[2] = 0xAA;
  4414.                           matrix[1] = matrix[3] = 0x55;
  4415.                           fg_setcolor(15);
  4416.                           fg_drect(0,49,0,49,matrix);
  4417.  
  4418.                           fg_waitkey();
  4419.  
  4420.                           fg_setmode(old_mode);
  4421.                           fg_reset();
  4422.                        }
  4423.  
  4424.  
  4425. MCGA and VGA 256-Color Graphics Modes
  4426.  
  4427.      The MCGA and VGA 256-color graphics modes (modes 19, 20, and 21) use an
  4428. eight-byte dithering matrix that Fastgraph treats as a four-row by two-column
  4429. array.  Each pixel in modes 19, 20, and 21 requires eight bits of video
  4430. memory, so each byte of the dithering matrix only holds a single pixel.  We
  4431. therefore need the two column dithering matrix to produce any type of dither
  4432. pattern.  The pixel representation of the dithering matrix would appear as
  4433. shown below on the left; its translation to numeric values appears on the
  4434. right.
  4435.  
  4436.  
  4437.                     [6]   B   W   [7]     [6]    9   15   [7]
  4438.  
  4439.                     [4]   W   B   [5]     [4]   15    9   [5]
  4440.  
  4441.                     [2]   B   W   [3]     [2]    9   15   [3]
  4442.  
  4443.                     [0]   W   B   [1]     [0]   15    9   [1]
  4444.  
  4445.                                         Chapter 6:  Graphics Fundamentals   87
  4446.  
  4447. The B pixels translate to color value 9 (light blue), and the W pixels
  4448. translate to color value 15 (bright white).  Example 6-13 uses mode 19 to
  4449. draw our light blue and bright white checkerboard pattern.
  4450.  
  4451.                                 Example 6-13.
  4452.  
  4453.             main()
  4454.             {
  4455.                char matrix[8];
  4456.                int old_mode;
  4457.  
  4458.                old_mode = fg_getmode();
  4459.                fg_setmode(19);
  4460.  
  4461.                matrix[0] = matrix[3] = matrix[4] = matrix[7] = 15;
  4462.                matrix[1] = matrix[2] = matrix[5] = matrix[6] =  9;
  4463.                fg_drect(0,49,0,49,matrix);
  4464.  
  4465.                fg_waitkey();
  4466.  
  4467.                fg_setmode(old_mode);
  4468.                fg_reset();
  4469.             }
  4470.  
  4471.  
  4472. Closing Remarks
  4473.  
  4474.      There are two other important items pertaining to the fg_drect and
  4475. fg_drectw routines.  These items apply regardless of which graphics video
  4476. mode is being used.
  4477.  
  4478.      First, the dithering matrix may not contain virtual color values.  That
  4479. is, the pixel color values stored in the dithering matrix must be between 0
  4480. and the maximum color value for the current video mode.  If any color value
  4481. is redefined using the fg_defcolor routine, Fastgraph always ignores the
  4482. redefinition and instead uses the actual color value.  Note this does not
  4483. apply to palette registers or video DAC registers, because in these cases we
  4484. are redefining the color associated with a color value and not the color
  4485. value itself.
  4486.  
  4487.      Second, Fastgraph aligns the dithering matrix to specific pixel rows.
  4488. Fastgraph draws the dithered rectangle starting with the pixel row specified
  4489. by the rectangle's lower limit (the maximum y coordinate for fg_drect, or the
  4490. minimum y coordinate for fg_drectw) and proceeds upward until reaching the
  4491. rectangle's upper limit.  In all cases the dithering matrix used by fg_drect
  4492. and fg_drectw contains four rows.  If we let r represent the pixel row
  4493. corresponding to the rectangle's lower limit, then the first row used in the
  4494. dithering matrix is r modulo 4 (assuming the dithering matrix rows are
  4495. numbered 0 to 3).  This alignment enables you to use the same dithering
  4496. matrix in multiple calls to fg_drect and fg_drectw for building an object of
  4497. adjacent dithered rectangles (for example, an L-shaped area) and still have
  4498. the dither pattern match where the rectangles intersect.
  4499. 88   Fastgraph User's Guide
  4500.  
  4501.  
  4502. Region Fill
  4503.  
  4504.      Fastgraph includes routines for filling arbitrary regions.  The fg_paint
  4505. routine fills a region with the current color value by specifying a screen
  4506. space point in the region's interior.  The fg_paintw routine likewise fills a
  4507. region, but it requires the interior point to be expressed in world space.
  4508. Neither routine changes the graphics cursor position.
  4509.  
  4510.      Each of these routines has two arguments that specify the (x,y)
  4511. coordinates of the interior point.  For the fg_paint routine, the arguments
  4512. are integer quantities.  For the fg_paintw routine, they are floating point
  4513. quantities.
  4514.  
  4515.      The region being filled must be a closed polygon whose boundary color is
  4516. different from that of the specified interior point.  The region may contain
  4517. holes (interior areas that will not be filled).  Fastgraph fills the region
  4518. by changing every interior pixel whose color is the same as the specified
  4519. interior point, to the current color.  If the interior point is already the
  4520. current color, the region fill routines do nothing.  It is important to note
  4521. the screen edges are not considered polygon boundaries, and filling an open
  4522. polygon will cause the fg_paint and fg_paintw routines to behave
  4523. unpredictably.
  4524.  
  4525.      Example 6-14 illustrates a simple use of the fg_paint routine in a 320
  4526. by 200 graphics mode.  The program uses fg_bestmode to select an available
  4527. video mode (if no 320 by 200 graphics mode is available, the program exits).
  4528. After establishing the selected video mode, the program uses the fg_move and
  4529. fg_drawrel routines to draw a hollow rectangle in color 10 and a hollow
  4530. diamond in color 9.  The diamond is drawn in the middle of the rectangle,
  4531. thus making it a hole with respect to the rectangle.  The program leaves
  4532. these shapes on the screen until a key is pressed.  At that time, it calls
  4533. the fg_paint routine to fill that part of the rectangle outside the diamond
  4534. with color 10.  After waiting for another keystroke, the program again uses
  4535. fg_paint to fill the interior of the diamond with color 15.  Finally, the
  4536. program waits for another keystroke, restores the original video mode and
  4537. screen attributes, and returns to DOS.
  4538.  
  4539.                                 Example 6-14.
  4540.  
  4541.              main()
  4542.              {
  4543.                 int old_mode, new_mode;
  4544.  
  4545.                 new_mode = fg_bestmode(320,200,1);
  4546.                 if (new_mode < 0) {
  4547.                    printf("This program requires a 320 x 200 ");
  4548.                    printf("graphics mode.\n");
  4549.                    exit();
  4550.                    }
  4551.  
  4552.                 old_mode = fg_getmode();
  4553.                 fg_setmode(new_mode);
  4554.  
  4555.                 fg_setcolor(10);
  4556.                 fg_move(100,50);
  4557.  
  4558.                                         Chapter 6:  Graphics Fundamentals   89
  4559.  
  4560.                 fg_drawrel(120,0);
  4561.                 fg_drawrel(0,100);
  4562.                 fg_drawrel(-120,0);
  4563.                 fg_drawrel(0,-100);
  4564.  
  4565.                 fg_setcolor(9);
  4566.                 fg_move(160,80);
  4567.                 fg_drawrel(30,20);
  4568.                 fg_drawrel(-30,20);
  4569.                 fg_drawrel(-30,-20);
  4570.                 fg_drawrel(30,-20);
  4571.                 fg_waitkey();
  4572.  
  4573.                 fg_setcolor(10);
  4574.                 fg_paint(160,70);
  4575.                 fg_waitkey();
  4576.  
  4577.                 fg_setcolor(15);
  4578.                 fg_paint(160,100);
  4579.                 fg_waitkey();
  4580.  
  4581.                 fg_setmode(old_mode);
  4582.                 fg_reset();
  4583.              }
  4584.  
  4585.  
  4586. Summary of Fundamental Graphics Routines
  4587.  
  4588.      This section summarizes the functional descriptions of the Fastgraph
  4589. routines presented in this chapter.  More detailed information about these
  4590. routines, including their arguments and return values, may be found in the
  4591. Fastgraph Reference Manual.
  4592.  
  4593.      FG_CIRCLE draws an unfilled circle in screen space.  The circle is
  4594. centered at the graphics cursor position.
  4595.  
  4596.      FG_CIRCLEW draws an unfilled circle in world space.  The circle is
  4597. centered at the graphics cursor position.
  4598.  
  4599.      FG_CLPRECT draws a solid (filled) rectangle in screen space, with
  4600. respect to the clipping region.
  4601.  
  4602.      FG_CLPRECTW draws a solid (filled) rectangle in world space, with
  4603. respect to the clipping region.
  4604.  
  4605.      FG_DASH draws a dashed line from the graphics cursor position to an
  4606. absolute screen space position.  It also makes the destination position the
  4607. new graphics cursor position.
  4608.  
  4609.      FG_DASHREL draws a dashed line from the graphics cursor position to a
  4610. screen space position relative to it.  It also makes the destination position
  4611. the new graphics cursor position.
  4612.  
  4613.      FG_DASHRW draws a dashed line from the graphics cursor position to a
  4614. world space position relative to it.  It also makes the destination position
  4615. the new graphics cursor position.
  4616. 90   Fastgraph User's Guide
  4617.  
  4618.      FG_DASHW draws a dashed line from the graphics cursor position to an
  4619. absolute world space position.  It also makes the destination position the
  4620. new graphics cursor position.
  4621.  
  4622.      FG_DRAW draws a solid line from the graphics cursor position to an
  4623. absolute screen space position.  It also makes the destination position the
  4624. new graphics cursor position.
  4625.  
  4626.      FG_DRAWREL draws a solid line from the graphics cursor position to a
  4627. screen space position relative to it.  It also makes the destination position
  4628. the new graphics cursor position.
  4629.  
  4630.      FG_DRAWRW draws a solid line from the graphics cursor position to a
  4631. world space position relative to it.  It also makes the destination position
  4632. the new graphics cursor position.
  4633.  
  4634.      FG_DRAWW draws a solid line from the graphics cursor position to an
  4635. absolute world space position.  It also makes the destination position the
  4636. new graphics cursor position.
  4637.  
  4638.      FG_DRECT draws a dithered rectangle in screen space, without regard to
  4639. the clipping region.  The rectangle's dither pattern is defined through a
  4640. dithering matrix whose format is dependent on the video mode being used.
  4641.  
  4642.      FG_DRECTW draws a dithered rectangle in world space, without regard to
  4643. the clipping region.  The rectangle's dither pattern is defined through a
  4644. dithering matrix whose format is dependent on the video mode being used.
  4645.  
  4646.      FG_ELLIPSE draws an unfilled ellipse in screen space.  The ellipse is
  4647. centered at the graphics cursor position, and its size is determined by the
  4648. specified lengths of the semi-axes.
  4649.  
  4650.      FG_ELLIPSEW draws an unfilled ellipse in world space.  The ellipse is
  4651. centered at the graphics cursor position, and its size is determined by the
  4652. specified lengths of the semi-axes.
  4653.  
  4654.      FG_ERASE clears the screen in either text or graphics modes.
  4655.  
  4656.      FG_GETPIXEL returns the color value of a specified pixel.
  4657.  
  4658.      FG_GETXPOS returns the screen space x coordinate of the graphics cursor
  4659. position.
  4660.  
  4661.      FG_GETYPOS returns the screen space y coordinate of the graphics cursor
  4662. position.
  4663.  
  4664.      FG_MOVE establishes the graphics cursor position at an absolute screen
  4665. space point.
  4666.  
  4667.      FG_MOVEREL establishes the graphics cursor position at a screen space
  4668. point relative to the current position.
  4669.  
  4670.      FG_MOVERW establishes the graphics cursor position at a world space
  4671. point relative to the current position.
  4672.  
  4673.      FG_MOVEW establishes the graphics cursor position at an absolute world
  4674. space point.
  4675.                                         Chapter 6:  Graphics Fundamentals   91
  4676.  
  4677.  
  4678.      FG_PAINT fills an arbitrary closed region with the current color value.
  4679. The region is defined by specifying a screen space point within its interior.
  4680.  
  4681.      FG_PAINTW fills an arbitrary closed region with the current color value.
  4682. The region is defined by specifying a world space point within its interior.
  4683.  
  4684.      FG_POINT draws a point (that is, displays a pixel) in screen space.
  4685.  
  4686.      FG_POINTW draws a point in world space.
  4687.  
  4688.      FG_POLYGON draws an unfilled polygon in screen space, using two
  4689. coordinate arrays to define the polygon vertices.  The drawing of the polygon
  4690. begins at the graphics cursor position, through the vertices defined by the
  4691. coordinate arrays, and finally back to the original cursor position if
  4692. necessary.
  4693.  
  4694.      FG_POLYGONW draws an unfilled polygon in world space.  It is identical
  4695. to the fg_polygon routine, except the coordinate arrays contain world space
  4696. values.
  4697.  
  4698.      FG_RECT draws a solid (filled) rectangle in screen space or character
  4699. space, without regard to the clipping region.
  4700.  
  4701.      FG_RECTW draws a solid (filled) rectangle in world space, without regard
  4702. to the clipping region.
  4703.  
  4704.      FG_SETCLIP defines the clipping region in screen space.  The clipping
  4705. region is a rectangular area outside of which graphics are suppressed.
  4706.  
  4707.      FG_SETCLIPW defines the clipping region in world space.
  4708. 92   Fastgraph User's Guide
  4709.  
  4710.  
  4711. Chapter 7
  4712.  
  4713. Character Display Routines
  4714. 94   Fastgraph User's Guide
  4715.  
  4716.  
  4717. Overview
  4718.  
  4719.      An important part of any program is the capability to display text or
  4720. other characters on the screen.  Fastgraph supports two character sets:  the
  4721. hardware or BIOS character set available with each video mode, and
  4722. Fastgraph's own software character set for graphics video modes.
  4723. Fastgraph/Light does not support the software character set.
  4724.  
  4725.      We'll begin this chapter with a review of character space, and then
  4726. discuss the specifics about hardware and software characters.  At the end of
  4727. the chapter, we'll briefly explain how to implement bit-mapped characters
  4728. into a program.  To simplify things, the example programs presented in this
  4729. chapter are mode-specific examples, and no testing is done to check if the
  4730. video mode is available on the user's system.
  4731.  
  4732.  
  4733. Character Space
  4734.  
  4735.      The coordinate system used for displaying hardware characters is called
  4736. character space.  It is the only coordinate system available in text video
  4737. modes, but it is a supplementary coordinate system you can use with either
  4738. screen space or world space in graphics video modes.  Character space can be
  4739. thought of as a grid of rows and columns, with each cell in the grid holding
  4740. one character.  Each cell is identified by its unique (row,column) integer
  4741. coordinates.  The rows and columns are numbered starting at zero; the origin
  4742. is always the upper left corner of the screen.  For example, in the 80-column
  4743. by 25-row video modes, the (row,column) coordinates of the screen corners are
  4744. shown in the following diagram.
  4745.  
  4746.  
  4747.                             (0,0)           (0,79)
  4748.  
  4749.  
  4750.  
  4751.                             (24,0)         (24,79)
  4752.  
  4753.  
  4754. The number of rows and columns depends on the video mode, as shown in the
  4755. following table.  For graphics modes, the table also includes the width and
  4756. height in pixels of a character cell.
  4757.                                    Chapter 7:  Character Display Routines   95
  4758.  
  4759.                      Mode  No. of  No. of Char. Char.
  4760.                     Number Columns Rows   Width Height
  4761.  
  4762.                        0     40      25
  4763.                        1     40      25
  4764.                        2     80      25
  4765.                        3     80      25
  4766.                        4     40      25     8     8
  4767.                        5     40      25     8     8
  4768.                        6     80      25     8     8
  4769.                        7     80      25
  4770.                        9     40      25     8     8
  4771.                       11     80      25     9     14
  4772.                       12     40      25     8     8
  4773.                       13     40      25     8     8
  4774.                       14     80      25     8     8
  4775.                       15     80      25     8     14
  4776.                       16     80      25     8     14
  4777.                       17     80      30     8     16
  4778.                       18     80      30     8     16
  4779.                       19     40      25     8     8
  4780.                       20     40      25     8     8
  4781.                       21     40      50     8     8
  4782.  
  4783.  
  4784.  
  4785. Hardware Characters
  4786.  
  4787.      Hardware characters are available in all of Fastgraph's supported video
  4788. modes.  As explained in chapter 5, text mode characters have a display
  4789. attribute that defines their foreground color, their background color, and
  4790. whether or not they blink.  Graphics mode characters appear in a single
  4791. color, as determined by the current color index.  Chapter 5 also explained
  4792. how Fastgraph's fg_setattr and fg_setcolor routines define the attribute or
  4793. color index in which subsequent hardware characters appear.
  4794.  
  4795.      It is obviously important to define the color or attribute for hardware
  4796. characters, but it is equally important to define their location on the
  4797. screen.  Fastgraph draws hardware characters at the position defined by the
  4798. text cursor.  Like the graphics cursor, the text cursor is not a cursor in
  4799. the true sense, but is simply a pair of character space (row,column)
  4800. coordinates with a special meaning.  The fg_setmode routine sets the text
  4801. cursor position to the character space coordinates (0,0), which of course is
  4802. the upper left corner of the screen.1
  4803.  
  4804.      The Fastgraph routine fg_locate changes the text cursor position.  It
  4805. has two integer arguments that specify the (row,column) character space
  4806. coordinates of the new position.  The row values must be between 0 and 24,
  4807. except in the 30-row VGA graphics modes (modes 17 and 18) where they must be
  4808. between 0 and 29.  The column values must be between 0 and 39 for 40-column
  4809. video modes, or between 0 and 79 for 80-column modes.
  4810. ____________________
  4811.  
  4812. 1 In reality there are 16 text cursors, one for each video page.  The setmode
  4813.   routine initializes each text cursor position to (0,0).  The next chapter
  4814.   describes this in more detail.
  4815.  
  4816. 96   Fastgraph User's Guide
  4817.  
  4818.      The fg_text routine is Fastgraph's fundamental character display
  4819. routine.  It displays a string of hardware characters, starting at the text
  4820. cursor position, using the current color attribute (for text modes) or color
  4821. index (for graphics modes).  If the string reaches the last column in a row,
  4822. fg_text will wrap the string to the first column of the next row.
  4823. Additionally, fg_text leaves the cursor one column to the right of the last
  4824. character displayed (or the first column of the next row if the last
  4825. character appears at the end of a row).  This feature makes it possible for
  4826. successive calls to fg_text to display adjacent strings.  The first argument
  4827. for the fg_text routine is a character string of arbitrary length, and the
  4828. second argument is an integer value that specifies the number of characters
  4829. to display from that string.
  4830.  
  4831.      Example 7-1 illustrates the use of the fg_locate and fg_text routines in
  4832. the 80 by 25 color text mode (mode 3).  After establishing the video mode and
  4833. making the BIOS cursor invisible, the program displays four strings with
  4834. different attributes.  The attributes are selected using the fg_setattr
  4835. routine, and the strings are displayed by the fg_text routine.  The first
  4836. string appears in yellow (attributes 14,0,0) in the upper left corner of the
  4837. screen; the fg_locate routine is not necessary because (0,0) is the default
  4838. text cursor position established by fg_setmode.  The second string appears in
  4839. light green (10,0,0) one space to the right of the first string.  Its
  4840. position relies on the fact fg_text sets the new text cursor position one
  4841. space to the right of the last character displayed (following the "w" of
  4842. "yellow" in this case).  The leading space in " green" leaves a space between
  4843. the first and second strings.  Similarly, the third string appears in
  4844. blinking light red (12,0,1) one space to the right of the second string.
  4845.  
  4846.      The program then uses the fg_locate routine to move the text cursor to
  4847. the lower left corner of the screen and displays the "Press any key" string.
  4848. This string is displayed with a light red foreground against a gray
  4849. background (12,7,0).  The extra spaces surrounding the string extend the
  4850. background color one character position to the left and right and make the
  4851. string more visually appealing.  Finally, once you press any key, the program
  4852. restores the original video mode and screen attributes before returning to
  4853. DOS.
  4854.  
  4855.                                  Example 7-1.
  4856.  
  4857.                       main()
  4858.                       {
  4859.                          int old_mode;
  4860.  
  4861.                          old_mode = fg_getmode();
  4862.                          fg_setmode(3);
  4863.                          fg_cursor(0);
  4864.  
  4865.                          fg_setattr(14,0,0);
  4866.                          fg_text("yellow",6);
  4867.  
  4868.                          fg_setattr(10,0,0);
  4869.                          fg_text(" green",6);
  4870.  
  4871.                          fg_setattr(12,0,1);
  4872.                          fg_text(" blinking",9);
  4873.  
  4874.                                    Chapter 7:  Character Display Routines   97
  4875.  
  4876.                          fg_setattr(12,7,0);
  4877.                          fg_locate(24,0);
  4878.                          fg_text(" Press any key. ",16);
  4879.                          fg_waitkey();
  4880.  
  4881.                          fg_setmode(old_mode);
  4882.                          fg_reset();
  4883.                       }
  4884.  
  4885.  
  4886.      The fg_where routine retrieves the text cursor position in its two
  4887. integer arguments.  This routine is not used as frequently as the fg_locate
  4888. and fg_text routines because more often than not your program will know the
  4889. text cursor position implicitly, or you'll know in advance the locations at
  4890. which text will be displayed.  The fg_where routine takes two integer
  4891. arguments passed as pointers (that is, by reference), and these two arguments
  4892. respectively receive the text cursor's current row and column position.
  4893.  
  4894.      Example 7-2 produces the same results as example 7-1, but it does so a
  4895. bit differently.  It uses its own routine, put_string, to display a string at
  4896. a specified row and column.  The put_string routine simply calls fg_locate to
  4897. establish the text cursor position and then calls fg_text to display the
  4898. string.  Note the use of the C library function strlen to determine the
  4899. string length passed to the fg_text routine.  Example 7-2 also uses the
  4900. fg_where routine to retrieve the new text cursor positions, which are then
  4901. passed to the put_string routine.
  4902.  
  4903.                                  Example 7-2.
  4904.  
  4905.                    main()
  4906.                    {
  4907.                       int old_mode;
  4908.                       int row, column;
  4909.  
  4910.                       old_mode = fg_getmode();
  4911.                       fg_setmode(3);
  4912.                       fg_cursor(0);
  4913.  
  4914.                       fg_setattr(14,0,0);
  4915.                       put_string("yellow",0,0);
  4916.  
  4917.                       fg_setattr(10,0,0);
  4918.                       fg_where(&row,&column);
  4919.                       put_string("green",row,column+1);
  4920.  
  4921.                       fg_setattr(12,0,1);
  4922.                       fg_where(&row,&column);
  4923.                       put_string("blinking",row,column+1);
  4924.  
  4925.                       fg_setattr(12,7,0);
  4926.                       put_string(" Press any key. ",24,0);
  4927.                       fg_waitkey();
  4928.  
  4929.                       fg_setmode(old_mode);
  4930.                       fg_reset();
  4931.                    }
  4932.  
  4933. 98   Fastgraph User's Guide
  4934.                    put_string(string,row,column)
  4935.                    char *string;
  4936.                    int row, column;
  4937.                    {
  4938.                       fg_locate(row,column);
  4939.                       fg_text(string,strlen(string));
  4940.                       return(0);
  4941.                    }
  4942.  
  4943.  
  4944.      Sometimes you may wish to change the display attribute of existing text,
  4945. such as when creating a shadow around the edges of a pop-up window.  The
  4946. Fastgraph routine fg_chgattr performs this function.  It applies the current
  4947. text display attribute (as defined in the most recent call to fg_setattr or
  4948. fg_setcolor) to a given number of characters, starting at the text cursor
  4949. position.  It leaves the text cursor one column to the right of the last
  4950. character changed (or the first column of the next row if the last character
  4951. is at the end of a row).  The fg_chgattr routine's argument specifies the
  4952. number of characters to change.  This routine has no effect in graphics video
  4953. modes.
  4954.  
  4955.      The Fastgraph routine fg_chgtext performs somewhat the opposite function
  4956. of fg_chgattr.  It displays new text but uses the display attributes already
  4957. assigned to the character cells where the text will appear.  The fg_chgtext
  4958. routine takes the same two arguments as the fg_text routine, displays the
  4959. characters starting at the text cursor position, and leaves the cursor one
  4960. column to the right of the last character displayed.  Like fg_chgattr,
  4961. fg_chgtext has no effect in graphics video modes.
  4962.  
  4963.      Example 7-3 illustrates the fg_chgattr and fg_chgtext routines.  It runs
  4964. in the 80-column color text mode (mode 3), but if we change the fg_setmode
  4965. argument it would also run in the monochrome text mode (mode 7).  The program
  4966. first displays the word "hello" in the upper left corner of the screen, using
  4967. a gray foreground and black background attribute.  After waiting for a
  4968. keystroke, the program calls fg_chgattr to make the word "hello" appear in
  4969. reverse video (that is, a black foreground and gray background attribute).
  4970. After a second keystroke, the program uses fg_chgtext to change the "h" of
  4971. "hello" to upper case.  Following this, the program returns to DOS.
  4972.  
  4973.                                  Example 7-3.
  4974.  
  4975.                          main()
  4976.                          {
  4977.                             int old_mode;
  4978.  
  4979.                             old_mode = fg_getmode();
  4980.                             fg_setmode(3);
  4981.                             fg_cursor(0);
  4982.  
  4983.                             fg_setattr(7,0,0);
  4984.                             fg_text("hello",5);
  4985.                             fg_waitkey();
  4986.  
  4987.                             fg_locate(0,0);
  4988.                             fg_setattr(0,7,0);
  4989.                             fg_chgattr(5);
  4990.                             fg_waitkey();
  4991.  
  4992.                                    Chapter 7:  Character Display Routines   99
  4993.  
  4994.                             fg_locate(0,0);
  4995.                             fg_chgtext("H",1);
  4996.                             fg_waitkey();
  4997.  
  4998.                             fg_setmode(old_mode);
  4999.                             fg_reset();
  5000.                          }
  5001.  
  5002.  
  5003.      Displaying hardware characters in graphics video modes is quite
  5004. different than doing so in text modes, although we use the same Fastgraph
  5005. routines in both cases.  Example 7-4 is similar to example 7-1, but it runs
  5006. in the EGA enhanced graphics mode (mode 16) instead of a text mode.  In
  5007. graphics modes, the fg_cursor routine has no effect, so we have omitted it
  5008. from the program.  Furthermore, characters cannot be displayed with a
  5009. blinking attribute, so we have likewise omitted the blinking characters (we
  5010. could simulate blinking by repetitively displaying and erasing them, but that
  5011. is beyond the scope of this example).  Because graphics mode characters only
  5012. have a foreground color, we had to simulate the gray background of the "Press
  5013. any key" string by first drawing a rectangle where that string appears.  The
  5014. differences between examples 7-4 and 7-1 hold for any graphics video mode,
  5015. not just mode 16.
  5016.  
  5017.                                  Example 7-4.
  5018.  
  5019.                       main()
  5020.                       {
  5021.                          int old_mode;
  5022.  
  5023.                          old_mode = fg_getmode();
  5024.                          fg_setmode(16);
  5025.  
  5026.                          fg_setcolor(14);
  5027.                          fg_text("yellow",6);
  5028.  
  5029.                          fg_setcolor(10);
  5030.                          fg_text(" green",6);
  5031.  
  5032.                          fg_setcolor(7);
  5033.                          fg_rect(0,127,336,349);
  5034.                          fg_setcolor(12);
  5035.                          fg_locate(24,0);
  5036.                          fg_text(" Press any key. ",16);
  5037.                          fg_waitkey();
  5038.  
  5039.                          fg_setmode(old_mode);
  5040.                          fg_reset();
  5041.                       }
  5042.  
  5043.  
  5044.      Example 7-5 demonstrates a side effect that occurs when displaying
  5045. characters in graphics modes.  This example uses the MCGA graphics mode (mode
  5046. 19) and displays two character strings at the same location.  If we were to
  5047. do this in a text mode, the first string would disappear once we displayed
  5048. the second string.  In graphics modes, however, the portions of the first
  5049. string not covered by characters from the second string are still visible.
  5050. The reason for this may not be apparent at first, but remember when we
  5051. 100   Fastgraph User's Guide
  5052.  
  5053. display characters in graphics modes, we aren't really displaying characters
  5054. but merely a pixel representation of the characters.  Fastgraph has no way to
  5055. distinguish such pixels from any other pixels.
  5056.  
  5057.                                  Example 7-5.
  5058.  
  5059.                          main()
  5060.                          {
  5061.                             int old_mode;
  5062.  
  5063.                             old_mode = fg_getmode();
  5064.                             fg_setmode(19);
  5065.  
  5066.                             fg_setcolor(14);
  5067.                             fg_text("yellow",6);
  5068.                             fg_locate(0,0);
  5069.                             fg_setcolor(10);
  5070.                             fg_text(" green",6);
  5071.                             fg_waitkey();
  5072.  
  5073.                             fg_setmode(old_mode);
  5074.                             fg_reset();
  5075.                          }
  5076.  
  5077.  
  5078.      To avoid this problem, the recommended procedure for displaying
  5079. characters in graphics modes is to first erase the area where the text will
  5080. appear.  The easiest way to do this is to use the fg_rect routine to draw a
  5081. rectangle in the background color.  In example 7-5, we could do this by
  5082. inserting the statements
  5083.  
  5084.                               fg_setcolor(0);
  5085.                               fg_rect(0,47,0,7);
  5086.  
  5087. immediately before the call to fg_locate.  The parameters passed to the
  5088. fg_rect routine represent the 48 by 8 pixel region that corresponds to the
  5089. first six character cells of row 0 in the 320 by 200 graphics modes.
  5090.  
  5091.  
  5092. Conversion Routines
  5093.  
  5094.      We have already mentioned Fastgraph includes routines for converting
  5095. coordinates between character space and screen space (see page 41).  In this
  5096. section, we will review these routines and then present an example that uses
  5097. some of them.
  5098.  
  5099.      The fg_xalpha and fg_yalpha routines convert screen space coordinates to
  5100. character space.  The fg_xalpha routine converts a screen space x coordinate
  5101. to the character space column that contains the coordinate.  Similarly, the
  5102. fg_yalpha routine converts a screen space y coordinate to the character space
  5103. row that contains the coordinate.
  5104.  
  5105.      The fg_xconvert and fg_yconvert routines convert character space
  5106. coordinates to screen space.  The fg_xconvert routine converts a character
  5107. space column to the screen space coordinate of its leftmost pixel.
  5108.  
  5109.                                   Chapter 7:  Character Display Routines   101
  5110.  
  5111. Similarly, the fg_yconvert routine converts a character space row to the
  5112. screen space coordinate of its top (lowest-numbered) pixel.
  5113.  
  5114.      On page 99, example 7-4 demonstrated how to display characters in a
  5115. graphics mode.  Because characters do not have a background color in graphics
  5116. modes, example 7-4 used the fg_rect routine to simulate a background color by
  5117. drawing a gray rectangle before displaying the text.  It was necessary to
  5118. determine the screen coordinates of the character cells so we could pass the
  5119. correct parameters to fg_rect.  By using the fg_xconvert and fg_yconvert
  5120. routines, we can let Fastgraph calculate the required screen coordinates.
  5121. This method has the additional benefit of working in any graphics mode,
  5122. whereas the coordinates passed to fg_rect in example 7-4 would only work
  5123. properly in a 640 by 350 graphics mode.  Example 7-6 shows how we could
  5124. extend example 7-4 to use the fg_xconvert and fg_yconvert routines.
  5125.  
  5126.                                  Example 7-6.
  5127.  
  5128.                       main()
  5129.                       {
  5130.                          int old_mode;
  5131.                          int minx, maxx, miny, maxy;
  5132.  
  5133.                          fg_old_mode = fg_getmode();
  5134.                          fg_setmode(16);
  5135.  
  5136.                          fg_setcolor(14);
  5137.                          fg_text("yellow",6);
  5138.  
  5139.                          fg_setcolor(10);
  5140.                          fg_text(" green",6);
  5141.  
  5142.                          fg_setcolor(7);
  5143.                          minx = fg_xconvert(0);
  5144.                          maxx = fg_xconvert(16) - 1;
  5145.                          miny = fg_yconvert(24);
  5146.                          maxy = fg_yconvert(25) - 1;
  5147.                          fg_rect(minx,maxx,miny,maxy);
  5148.                          fg_setcolor(12);
  5149.                          fg_locate(24,0);
  5150.                          fg_text(" Press any key. ",16);
  5151.                          fg_waitkey();
  5152.  
  5153.                          fg_setmode(old_mode);
  5154.                          fg_reset();
  5155.                       }
  5156.  
  5157.  
  5158.  
  5159. Software Characters
  5160.  
  5161.      Software characters, also called stroke characters or vector characters
  5162. in other literature, are only are available in graphics video modes.  Unlike
  5163. the fixed-size hardware characters, you can display software characters in
  5164. any size, at any angle, and at any position.  In addition, software
  5165. characters are proportionally spaced.  However, software characters take
  5166. considerably longer to draw than do hardware characters.
  5167. 102   Fastgraph User's Guide
  5168.  
  5169.      Fastgraph includes two software character fonts, called the primary font
  5170. and the alternate font.  The primary font contains upper and lower case
  5171. letters, numbers, punctuation, and most of the other printable ASCII
  5172. characters.  The alternate font contains upper and lower case Greek letters
  5173. and other mathematical and scientific symbols.
  5174.  
  5175.      The Fastgraph routine fg_swchar displays a string of software characters
  5176. in the current color index (as defined by the most recent call to
  5177. fg_setcolor).  The string may contain any characters from the primary font,
  5178. the alternate font, or both.  You can display the characters left justified,
  5179. centered, or right justified relative to the graphics cursor position.  Just
  5180. as the fg_text routine updates the text cursor position, fg_swchar sets the
  5181. graphics cursor position just to the right of the last character drawn.  The
  5182. characters are clipped according to the current clipping region.  In addition
  5183. to the characters, the string passed to fg_swchar may also contain operators
  5184. for switching fonts, underlining, subscripting, or superscripting characters.
  5185. Because fg_swchar internally uses world space coordinates, you must call the
  5186. fg_initw routine at some point in your program before the first call to
  5187. fg_swchar.  You must also establish a world space coordinate system with the
  5188. fg_setworld routine.
  5189.  
  5190.      The fg_swchar routine has three arguments.  The first argument is the
  5191. character string to display.  The second argument is an integer value that
  5192. specifies the number of characters in the string, including any characters
  5193. used as special operators.  The third argument is an integer value that
  5194. determines the position of the string relative to the graphics cursor
  5195. position.  If this value is negative, the lower left corner of the first
  5196. character will be at the graphics cursor position.  If it is positive, the
  5197. lower right corner of the last character will be at the graphics cursor
  5198. position.  If it is zero, the string will be horizontally centered at the
  5199. graphics cursor position.
  5200.  
  5201.      The size of software characters is determined by the values passed to
  5202. the fg_setsize, fg_setsizew, and fg_setratio routines.  The fg_setsize
  5203. routine has a single integer argument that defines the height of software
  5204. characters in screen space units, while the fg_setsizew routine has a single
  5205. floating point argument that defines the height in world space units.  If
  5206. neither of these routines is called, Fastgraph will use its default character
  5207. height of one world space unit.  The fg_setratio routine has a single
  5208. floating point argument that defines the aspect ratio for software
  5209. characters.  The aspect ratio is the ratio of character width to character
  5210. height.  For example, an aspect ratio of 2.0 means characters are twice as
  5211. wide as they are high.  If the fg_setratio routine is not called, Fastgraph
  5212. uses its default aspect ratio of 1.
  5213.  
  5214.      Example 7-7 displays both of the software character fonts.  The program
  5215. uses the enhanced EGA graphics mode (mode 16), but it could run in any
  5216. graphics mode by changing the fg_setmode argument.  After establishing the
  5217. video mode, the program calls the fg_initw routine to initialize Fastgraph's
  5218. world space parameters; this is required since the software character drawing
  5219. routines internally use world space coordinates.  The next statement is a
  5220. call to fg_setworld that establishes a world space coordinate system with
  5221. 0.01 world space units per pixel.  Following this is a call to fg_setsizew
  5222. that defines the character height as 0.21 world space units, or 21 pixels.
  5223. Note we could have instead used the fg_setsize routine here with an integer
  5224. argument of 21.
  5225.                                   Chapter 7:  Character Display Routines   103
  5226.  
  5227.      The next part of the program draws the characters in the primary font on
  5228. the upper half of the screen.  After doing this, the program draws the
  5229. alternate font characters on the lower half.  In each case it does this with
  5230. the fg_swchar routine.  By default, the string passed to fg_swchar will
  5231. produce characters from the primary font.  However, you can insert a back
  5232. slash character (\) in the string to toggle between the two fonts.  Don't
  5233. forget the C language applies a special meaning to the back slash character
  5234. within strings, so you must use two consecutive back slashes to insert a
  5235. single back slash in the string.
  5236.  
  5237.                                  Example 7-7.
  5238.  
  5239.             main()
  5240.             {
  5241.                int old_mode;
  5242.  
  5243.                old_mode = fg_getmode();
  5244.                fg_setmode(16);
  5245.                fg_initw();
  5246.                fg_setworld(0.0,6.39,0.0,3.49);
  5247.                fg_setsizew(0.21);
  5248.  
  5249.                fg_setcolor(15);
  5250.                fg_locate(0,26);
  5251.                fg_text("Software characters - font 1",28);
  5252.  
  5253.                fg_setcolor(10);
  5254.                fg_movew(0.0,3.1);
  5255.                fg_swchar("ABCDEFGHIJKLMNOPQRSTUVWXYZ",26,-1);
  5256.                fg_movew(0.0,2.8);
  5257.                fg_swchar("abcdefghijklmnopqrstuvwxyz",26,-1);
  5258.                fg_movew(0.0,2.5);
  5259.                fg_swchar("0123456789",10,-1);
  5260.                fg_movew(0.0,2.2);
  5261.                fg_swchar("!\"#$%&'()*+,-./:;<=>?[]^`{|}~",29,-1);
  5262.  
  5263.                fg_setcolor(15);
  5264.                fg_locate(12,26);
  5265.                fg_text("Software characters - font 2",28);
  5266.  
  5267.                fg_setcolor(10);
  5268.                fg_movew(0.0,1.4);
  5269.                fg_swchar("\\ABCDEFGHIJKLMNOPRSTUWXYZ",25,-1);
  5270.                fg_movew(0.0,1.1);
  5271.                fg_swchar("\\abcdefghijklmnoprstuwxyz",25,-1);
  5272.                fg_movew(0.0,0.4);
  5273.                fg_swchar("\\012345678#$%&()*+/<=>?[]{}",27,-1);
  5274.  
  5275.                fg_waitkey();
  5276.  
  5277.                fg_setmode(old_mode);
  5278.                fg_reset();
  5279.             }
  5280.  
  5281.  
  5282.      Example 7-7 displays all characters in each font.  If you compare the
  5283. primary font strings with the alternate font strings, you'll see the
  5284. 104   Fastgraph User's Guide
  5285.  
  5286. alternate font contains fewer characters.  For example, the letters Q and V
  5287. (either upper or lower case) have no corresponding character in the alternate
  5288. font.  You might have also noticed the primary font does not support the full
  5289. printable ASCII character set.  Any character in a string passed to the
  5290. fg_swchar routine that does not have a corresponding character in the current
  5291. font will display a blank character.
  5292.  
  5293.      In addition to the font selection operator (the back slash character),
  5294. fg_swchar recognizes three other operators.  The superscript operator is a
  5295. back slash followed by a caret (\^).  It causes the next character to appear
  5296. as a superscript.  Similarly, the subscript operator is a back slash followed
  5297. by a lower case v (\v); it causes the next character to appear as a
  5298. subscript.  The size of superscripted and subscripted characters is one half
  5299. the height of the other characters.  The underline operator is the underscore
  5300. character (_).  It causes all subsequent characters in the string to be
  5301. underlined until another underscore character is found, or until the end of
  5302. the string.  When using these operators, be sure to include them as part of
  5303. the string length count passed to fg_swchar.
  5304.  
  5305.      Example 7-8 illustrates the use of the font selection, superscript,
  5306. subscript, and underline operators with the fg_swchar routine.  Again,
  5307. because the back slash character has a special meaning to the C programming
  5308. language, we must use two consecutive back slashes to represent a single back
  5309. slash within the string.  The program displays four strings:
  5310.  
  5311.                               cos²o + sin²o = 1
  5312.                                      H2O
  5313.                                      U232
  5314.                            One word is underlined.
  5315.  
  5316. The theta symbol (o) in the first string is produced by displaying the
  5317. character "h" in the alternate font.  Note another font selection operator
  5318. (\) appears immediately after the "h" to revert to the primary font.  The
  5319. first string also includes superscript operators (\^) to display the
  5320. exponents in the equation.  The second string includes a single subscripted
  5321. character, while the third string shows how to display three consecutive
  5322. subscripted characters.  Finally, the fourth string illustrates how to
  5323. underline characters.
  5324.  
  5325.      Note example 7-8 also uses the fg_setratio routine.  The first three
  5326. strings are drawn with an aspect ratio of 2, making them twice as wide as
  5327. they are high.  The fourth string is drawn with an aspect ratio of 1
  5328. (Fastgraph's default aspect ratio for software characters), so the character
  5329. height is the same as the character width.  Also, the strings are centered
  5330. instead of left justified as in the previous example.
  5331.  
  5332.                                  Example 7-8.
  5333.  
  5334.             main()
  5335.             {
  5336.                int old_mode;
  5337.  
  5338.                old_mode = fg_getmode();
  5339.                fg_setmode(16);
  5340.                fg_setcolor(10);
  5341.                fg_initw();
  5342.  
  5343.                                   Chapter 7:  Character Display Routines   105
  5344.  
  5345.                fg_setworld(0.0,6.39,0.0,3.49);
  5346.                fg_setratio(2.0);
  5347.                fg_setsizew(0.21);
  5348.  
  5349.                fg_movew(3.2,3.0);
  5350.                fg_swchar("cos\\^2\\h\\ + sin\\^2\\h\\ = 1",25,0);
  5351.  
  5352.                fg_movew(3.2,2.0);
  5353.                fg_swchar("H\\v2O   U\\v2\\v3\\v2",18,0);
  5354.  
  5355.                fg_movew(3.2,1.0);
  5356.                fg_setratio(1.0);
  5357.                fg_swchar("One _word_ is underlined.",25,0);
  5358.  
  5359.                fg_waitkey();
  5360.  
  5361.                fg_setmode(old_mode);
  5362.                fg_reset();
  5363.             }
  5364.  
  5365.      The fg_setangle routine defines the angle or orientation at which
  5366. software characters are displayed.  Its only argument is a floating point
  5367. value that specifies the angle, measured in degrees counterclockwise from the
  5368. positive x axis.  If a program draws software characters before calling
  5369. fg_setangle, Fastgraph will use its default angle of zero degrees (that is,
  5370. the characters will be oriented horizontally).
  5371.  
  5372.      In most programs, the alternate font is not needed.  However, if you use
  5373. the fg_swchar routine, Fastgraph will include the definitions of these
  5374. characters in your program's data segment.  To prevent wasting this space,
  5375. Fastgraph includes the fg_swtext routine.  The fg_swtext routine is identical
  5376. in all respects to the fg_swchar routine, except it does not include the
  5377. alternate font.  Since the font selection operator does not apply when using
  5378. fg_swtext, the routine simply ignores it.  You should only use fg_swtext if
  5379. do not use fg_swchar.  If you use both routines, your program will still work
  5380. correctly, but its data segment will contain a duplicate copy of the primary
  5381. font definitions.
  5382.  
  5383.      Example 7-9 demonstrates the use of the fg_setangle and fg_swtext
  5384. routines.  The program draws a series of strings of the form "nnn degrees",
  5385. where nnn is a multiple of 15, radiating from the screen center.  Each string
  5386. appears at the specified angle.  For example, the string "15 degrees" is
  5387. drawn at an angle of 15 degrees.
  5388.  
  5389.                                  Example 7-9.
  5390.  
  5391.                main()
  5392.                {
  5393.                   char string[24];
  5394.                   int angle;
  5395.                   int old_mode;
  5396.  
  5397.                   old_mode = fg_getmode();
  5398.                   fg_setmode(16);
  5399.                   fg_setcolor(10);
  5400.                   fg_initw();
  5401.  
  5402. 106   Fastgraph User's Guide
  5403.  
  5404.                   fg_setworld(0.0,6.39,0.0,3.49);
  5405.                   fg_setsizew(0.21);
  5406.  
  5407.                   for (angle = 0; angle < 360; angle += 15) {
  5408.                      fg_movew(3.2,1.75);
  5409.                      fg_setangle((double)angle);
  5410.                      sprintf(string,"     %3d degrees",angle);
  5411.                      fg_swtext(string,16,-1);
  5412.                      }
  5413.  
  5414.                   fg_waitkey();
  5415.  
  5416.                   fg_setmode(old_mode);
  5417.                   fg_reset();
  5418.                }
  5419.  
  5420.      The final routine pertaining to software characters is fg_swlength,
  5421. which returns the length of a specified string of software characters in
  5422. world space units.  The length is returned as the routine's floating point
  5423. function value.  The fg_swlength routine has two arguments -- a string of
  5424. software characters, and an integer value specifying the number of characters
  5425. in the string.  As with fg_swchar and fg_swtext, the count includes any of
  5426. the special operator characters.
  5427.  
  5428.      Example 7-10 demonstrates a typical use of the fg_swlength routine.  The
  5429. program displays the string "hello there." in light green against a gray
  5430. background in the middle of the screen.  As in our previous software
  5431. character examples, the program uses mode 16 and first performs the necessary
  5432. initializations to use software characters.  Following this, the program uses
  5433. the fg_swlength routine to compute the length in world space units of the
  5434. string.  Note we have added blank characters to each end of the string passed
  5435. to fg_swlength; this increases the length of the actual string and will
  5436. effectively give the gray rectangle an extended border on its left and right
  5437. sides.  The string length returned by fg_swlength is multiplied by 0.5,
  5438. giving the distance from the middle of the screen to either side of the
  5439. rectangle.  The program then uses this value to compute the minimum and
  5440. maximum x coordinates passed to fg_rectw.  After drawing the gray rectangle,
  5441. the program uses fg_swtext to draw the string of software characters in the
  5442. middle of the screen.  It then waits for a keystroke before restoring the
  5443. original video mode and screen attributes and returning to DOS.
  5444.  
  5445.                                 Example 7-10.
  5446.  
  5447.               main()
  5448.               {
  5449.                  int old_mode;
  5450.                  double half;
  5451.                  double fg_swlength();
  5452.  
  5453.                  old_mode = fg_getmode();
  5454.                  fg_setmode(16);
  5455.                  fg_initw();
  5456.                  fg_setworld(0.0,6.39,0.0,3.49);
  5457.                  fg_setsizew(0.21);
  5458.  
  5459.                  fg_setcolor(7);
  5460.  
  5461.                                   Chapter 7:  Character Display Routines   107
  5462.  
  5463.                  half = fg_swlength(" Hello there. ",14) * 0.5;
  5464.                  fg_rectw(3.2-half,3.2+half,1.6,1.9);
  5465.  
  5466.                  fg_setcolor(10);
  5467.                  fg_movew(3.2,1.65);
  5468.                  fg_swtext("Hello there.",12,0);
  5469.  
  5470.                  fg_waitkey();
  5471.  
  5472.                  fg_setmode(old_mode);
  5473.                  fg_reset();
  5474.               }
  5475.  
  5476.  
  5477. Bit-Mapped Characters
  5478.  
  5479.      Bit-mapped characters combine the properties of hardware and software
  5480. characters.  They are a fixed size, as hardware characters are, but they can
  5481. be positioned anywhere instead of just in character cells.  Because they are
  5482. not scalable, they do not require floating point arithmetic, and therefore
  5483. they are much faster to display than software characters.
  5484.  
  5485.      Fastgraph makes no special provision for bit-mapped characters because
  5486. it treats them as if they were any other bit-mapped image.  For example, to
  5487. use a five-pixel by five-pixel bit-mapped font, you can construct characters
  5488. as shown below and then store these representations in an image array.
  5489.  
  5490.                           *       * * * *       * * * *
  5491.                         *   *     *       *   *
  5492.                       * * * * *   * * * *     *
  5493.                       *       *   *       *   *
  5494.                       *       *   * * * *       * * * *
  5495.  
  5496. The image display routines fg_drawmap and fg_drwimage, discussed in chapter
  5497. 9, could then be used to display specific characters from the image array.
  5498.  
  5499.  
  5500. Summary of Character Display Routines
  5501.  
  5502.      This section summarizes the functional descriptions of the Fastgraph
  5503. routines presented in this chapter.  More detailed information about these
  5504. routines, including their arguments and return values, may be found in the
  5505. Fastgraph Reference Manual.
  5506.  
  5507.      FG_CHGATTR applies the current text display attribute to a given number
  5508. of characters, starting at the text cursor position.  This routine leaves the
  5509. text cursor one column to the right of the last character changed (or the
  5510. first column of the next row if the last character is at the end of a row).
  5511. It has no effect in graphics video modes.
  5512.  
  5513.      FG_CHGTEXT displays a string of hardware characters, starting at the
  5514. text cursor position, using the existing text display attributes.  This
  5515. routine leaves the text cursor one column to the right of the last character
  5516. displayed (or the first column of the next row if the last character is at
  5517. the end of a row).  It has no effect in graphics video modes.
  5518.  
  5519. 108   Fastgraph User's Guide
  5520.  
  5521.      FG_LOCATE changes the text cursor position for the active video page.'
  5522.  
  5523.      FG_SETANGLE defines the angle or orientation at which software
  5524. characters are displayed.  The angle is measured in degrees counterclockwise
  5525. from the positive x axis.
  5526.  
  5527.      FG_SETATTR establishes the current text display attribute in text video
  5528. modes.  This routine has no effect in graphics video modes.
  5529.  
  5530.      FG_SETCOLOR establishes the current color index (which may be a virtual
  5531. color index in graphics modes).  In text modes, the fg_setcolor routine
  5532. provides an alternate method of establishing the current text display
  5533. attribute.
  5534.  
  5535.      FG_SETRATIO defines the aspect ratio for software characters.  The
  5536. aspect ratio is the ratio of character width to character height.
  5537.  
  5538.      FG_SETSIZE defines the height of software characters in screen space
  5539. units.
  5540.  
  5541.      FG_SETSIZEW defines the height of software characters in world space
  5542. units.
  5543.  
  5544.      FG_SWCHAR displays a string of software characters using the current
  5545. color index.  The string may be left justified, centered, or right justified
  5546. relative to the graphics cursor position.  The string passed to fg_swchar may
  5547. contain special operators that allow switching between fonts, underlining,
  5548. superscripting, or subscripting.  This routine has no effect in text video
  5549. modes.
  5550.  
  5551.      FG_SWLENGTH returns the length in world space units of a string of
  5552. software characters.
  5553.  
  5554.      FG_SWTEXT is a scaled down version of the fg_swchar routine.  It does
  5555. not include the alternate font character definitions and thus requires less
  5556. memory than fg_swchar.
  5557.  
  5558.      FG_TEXT displays a string of hardware characters, starting at the text
  5559. cursor position, using the current color attribute (for text modes) or color
  5560. index (for graphics modes).  This routine leaves the text cursor one column
  5561. to the right of the last character displayed (or the first column of the next
  5562. row if the last character is at the end of a row).
  5563.  
  5564.      FG_WHERE retrieves the row and column numbers of the text cursor
  5565. position.
  5566.  
  5567.      FG_XALPHA and FG_YALPHA convert screen space coordinates to character
  5568. space.
  5569.  
  5570.      FG_XCONVERT and FG_YCONVERT convert character space coordinates to
  5571. screen space.
  5572.